numpy.savez(file, *args, **kwds)

Save several arrays into a single, compressed file in .npz format.

If keyword arguments are given, the names for variables assigned to the keywords are the keyword names (not the variable names in the caller). If arguments are passed in with no keywords, the corresponding variable names are arr_0, arr_1, etc.


file : str or file

Either the file name (string) or an open file (file-like object) If file is a string, it names the output file. “.npz” will be appended to the file name if it is not already there.

args : Arguments

Any function arguments other than the file name are variables to save. Since it is not possible for Python to know their names outside savez, they will be saved with names “arr_0”, “arr_1”, and so on. These arguments can be any expression.

kwds : Keyword arguments

All keyword=value pairs cause the value to be saved with the name of the keyword.

See also

Save a single array to a binary file in NumPy format.
Save an array to a file as plain text.


The .npz file format is a zipped archive of files named after the variables they contain. Each file contains one variable in .npy format. For a description of the .npy format, see format.


>>> from tempfile import TemporaryFile
>>> outfile = TemporaryFile()
>>> x = np.arange(10)
>>> y = np.sin(x)

Using savez with *args, the arrays are saved with default names.

>>> np.savez(outfile, x, y)
>>>  # only necessary in this example (with tempfile)
>>> npz = np.load(outfile)
>>> npz.files
['arr_1', 'arr_0']
>>> npz['arr_0']
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

Using savez with **kwds, the arrays are saved with the keyword names.

>>> outfile = TemporaryFile()
>>> np.savez(outfile, x=x, y=y)
>>> npz = np.load(outfile)
>>> npz.files
['y', 'x']
>>> npz['x']
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

Previous topic

Next topic


This Page