shaare.it

ValueError: file is not a NumPy file / cannot read header

7 Dec 2025

2 min read

ValueError: file is not a NumPy file / cannot read header

$ python - <<'PY'
import numpy as np
# create a plain text file then try to load it as an .npy
open('bad.npy', 'wb').write(b'hello world')
np.load('bad.npy')
PY
Traceback (most recent call last):
  File "<string>", line 4, in <module>
ValueError: The file is not a numpy file

Why this happens

NumPy .npy/.npz files use a binary format with a specific header. Loading a file that wasn’t saved with NumPy (or is corrupted/truncated) will cause np.load to fail with a ValueError or EOFError while parsing the header.

Fix

  • Ensure the file you are loading was generated by np.save, np.savez or is a valid .npy/.npz file.
  • If the file is a text file with tabular data, use np.loadtxt, np.genfromtxt, pandas, or the appropriate loader instead of np.load.
  • For zipped or zipped+pickled files (.npz, .npy), check whether allow_pickle is required when the array contains object dtypes.

Wrong code

import numpy as np
# Trying to load a text file with the binary loader
open('bad.npy', 'wb').write(b'1,2,3\n4,5,6')
arr = np.load('bad.npy')

Fixed code

import numpy as np
# Correct: load text files with loadtxt
open('data.txt', 'w').write('1,2,3\n4,5,6')
arr = np.loadtxt('data.txt', delimiter=',')
print(arr)

# Or save properly as .npy
arr2 = np.array([1,2,3])
np.save('arr.npy', arr2)
np.load('arr.npy')