Skip to main content

Module npz

Module npz 

Source
Expand description

NumPy .npz archives (numpy.savez, numpy.savez_compressed and numpy.load).

An .npz file is a ZIP archive whose members are .npy files named <name>.npy. This module reads and writes the ZIP container itself:

  • The reader finds the end-of-central-directory record, and reads each member’s name, method, sizes, CRC-32 and offset from the central directory, not from the local headers, which may hold zeros when a data descriptor follows the data. ZIP64 (sizes and offsets above 4 GiB, or 65 535 members and more) is read. Members may be stored (method 0) or deflated (8); each is checked against its CRC-32 and its declared size.
  • The writer stores (savez) or deflates (savez_compressed) each member, and uses ZIP64 records exactly when a size or an offset needs them.
  • Safety: encrypted members, other compression methods, path-traversal and duplicate names, truncated archives, size and CRC mismatches and decompression bombs are errors, never panics. Nothing is written to the file system by name.

See the book entry Data files.

Structs§

NpzArchive
A parsed .npz archive, borrowing its bytes. Members are decoded on demand.

Functions§

npz_decode
Reads every .npy member of an .npz archive, as (name, array) pairs in archive order with .npy removed from the names. Members of other names are skipped.
npz_encode
Builds an .npz archive from named arrays (numpy.savez, or numpy.savez_compressed when compressed): each array becomes the member <name>.npy. Names must be non-empty, distinct, and free of /, \, .. and a trailing .npy. Sizes and offsets that do not fit 32 bits get ZIP64 records.
npz_encode_positional
As npz_encode, for arrays without names: they are called arr_0, arr_1, … as numpy.savez does.
npz_encode_zip64
As npz_encode, with ZIP64 records on every member and on the end of the directory even though the sizes do not need them (what NumPy’s savez does for the local headers). Mainly for testing readers.
read_npz_file
Reads an .npz file.
write_npz_file
Writes an .npz file.