← Back to NumPy Course | Chapter 11: File I/O | Lesson 1 of 5

np.save/load

np.save writes an array to a compact binary .npy file and np.load reads it back exactly as it was.

In this page:

  1. np.save/load
Syntax
python
np.save("file.npy", arr)
arr = np.load("file.npy")

np.save/load

The .npy format stores the array with its dtype and shape, so nothing is lost on reload. np.savez bundles several arrays into one .npz file. Binary files are smaller and faster than text but not human-readable.

Note: np.save adds the .npy extension for you if you leave it off.

Example: np.save/load

python
import numpy as np
import os
import tempfile

a = np.arange(6).reshape(2, 3)
path = os.path.join(tempfile.mkdtemp(), "data.npy")
np.save(path, a)
b = np.load(path)
print(b)
print("identical:", np.array_equal(a, b))

# Output:
# [[0 1 2]
#  [3 4 5]]
# identical: True
Related Topics
Common Mistakes
  1. Expecting a .npy file to be readable in a text editor
  2. Forgetting allow_pickle for object arrays
  3. Overwriting an existing file unintentionally
Chapter Summary
  • save writes a .npy binary file
  • load restores dtype and shape
  • savez stores several arrays
  • Not human-readable
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.