Skip to content

Instantly share code, notes, and snippets.

Created July 13, 2016 04:42
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
.npy info #numpy
import struct, ast
def npy_info(fh):
"""Get information about a .npy file, without reading in all the data.
# Check magic number
if != b'\x93NUMPY':
raise IOError('Magic number not found, file is not .npy format or is corrupt')
# Get version
major, minor = struct.unpack('bb',
# Header length
if major == 1:
header_len, = struct.unpack('<h',
header_len, = struct.unpack('<i',
# Header
header_str =
header = ast.literal_eval(header_str)
return dict(
version=(major, minor),
data_offset=10 + header_len,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment