Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

NumPy · Function

np.save

Saves array-like data to NumPy’s binary .npy format. It accepts paths or file objects and optionally permits pickle serialization for object arrays.

How it works

The function serializes array-like data into NumPy's binary .npy format. A path-like filename receives the .npy suffix when needed, while a file object keeps its existing name; object arrays may be written through Python pickling when allowed.

Syntax

numpy.save(file, arr, allow_pickle=True)

Parameters

NameTypeDescription
filefile, str, or pathlib.PathFile or filename to which the data is saved. A file object, string, or pathlib.Path is accepted; a suffix may be added for path-based names.
arrarray_likeArray data to be saved.
allow_picklebool, optionalWhether object arrays may be saved using Python pickles. Default: True.

Key points

  • Use np.save for NumPy's binary .npy representation when you need to persist one array in a format designed for NumPy.
  • The destination may be an open file, a string filename, or a pathlib.Path.
  • Path-based destinations are normalized with the .npy suffix unless that suffix is already present.
  • arr can be array-like, so callers can provide compatible sequence data rather than only an existing ndarray.
  • allow_pickle determines whether arrays containing Python objects may use pickle serialization.
  • Passing an open file gives the caller control over the stream and avoids automatic filename modification.

Common mistakes

  • The default permits pickle-backed object arrays, so set allow_pickle=False when untrusted data or security constraints make pickle unsafe.
  • Saving repeatedly to the same open file appends additional .npy records; a reader must handle the resulting sequence rather than assume one record.

Example: Save and reload an array

import os
import tempfile
import numpy as np

with tempfile.TemporaryDirectory() as directory:
    path = os.path.join(directory, "values")
    values = np.array([[1, 2], [3, 4]])
    np.save(path, values)
    restored = np.load(path + ".npy")
    print(restored)

Creates a two-dimensional array, saves it as values.npy, and loads it back.

When to use it

Save computed arrays

Persisting numerical results between program runs

Use the NumPy-native binary format

Exchanging ndarray data between Python processes

Append multiple saves

Writing several array records to one stream

Interview questions

QWhat does np.save do?

np.save writes array-like data in NumPy's binary .npy format. With a string or Path, NumPy adds .npy if the name does not already have that extension.

Follow-up: What happens with an already-open file object? Its filename is unchanged, and successive saves append data to the file.

QWhat are the parameters of np.save?

file accepts a file object, string, or pathlib.Path, while arr accepts array-like data. allow_pickle controls whether object arrays can be saved using Python pickles and defaults to True.

Follow-up: Why might allow_pickle be False? Pickle loading can execute arbitrary code, and pickled objects can be less portable across Python installations.

QWhat does np.save return, and how can it store multiple arrays?

The function's documented effect is writing the array to a .npy file; the page does not document a return value, so it returns None. The format preserves NumPy array data for later loading.

Follow-up: How can multiple arrays be stored in one file? Use an open file object and call np.save repeatedly; each saved record is appended.

  • numpy.load
  • numpy.savez
  • numpy.savez_compressed

Source: numpy.save. Checked 2026-10-15.