Error loading numpy file
I tried to load a .npy file generated by numpy:
import numpy as np
F = np.load('file.npy')
And numpy throws this error:
C: \ Miniconda3 \ lib \ site-packages \ numpy \ lib \ npyio.py in download (file, mmap_mode)
379 N = len(format.MAGIC_PREFIX) 380 magic = fid.read(N)
-> 381 fid.seek (-N, 1) # backup
382 if magic.startswith(_ZIP_PREFIX): 383 # zip-file (assume .npz)
OSError: [Errno 22] Invalid argument
Can anyone explain to me what this means? How do I recover a file?
source to share
You are using a file object that does not support the method seek
. Note that the parameter file
numpy.load
must support the methodseek
. I am assuming that you are probably working with a file object that corresponds to another file object that has already been opened elsewhere and remains open:
>>> f = open('test.npy', 'wb') # file remains open after this line
>>> np.load('test.npy') # numpy now wants to use the same file
# but cannot apply `seek` to the file opened elsewhere
Traceback (most recent call last):
File "<pyshell#114>", line 1, in <module>
np.load('test.npy')
File "C:\Python27\lib\site-packages\numpy\lib\npyio.py", line 370, in load
fid.seek(-N, 1) # back-up
IOError: [Errno 22] Invalid argument
Please note that I am getting the same error as you. If you have an open file object, you must close it before using it np.load
and before using np.save
it to save the file.
source to share