user2863620
user2863620

Reputation: 643

numpy loading file error

I tried to load .npy file created by numpy:

import numpy as np

F = np.load('file.npy')

And numpy raises this error:

C:\Miniconda3\lib\site-packages\numpy\lib\npyio.py in load(file, mmap_mode)

379         N = len(format.MAGIC_PREFIX)
380         magic = fid.read(N)

--> 381 fid.seek(-N, 1) # back-up

382         if magic.startswith(_ZIP_PREFIX):
383             # zip-file (assume .npz)

OSError: [Errno 22] Invalid argument

Could anyone explain me what its mean? How can I recover my file?

Upvotes: 3

Views: 5659

Answers (1)

Justin O Barber
Justin O Barber

Reputation: 11591

You are using a file object that does not support the seek method. Note that the file parameter of numpy.load must support the seek method. My guess is that you are perhaps operating on 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

Note that I receive the same error as you did. If you have an open file object, you will want to close it before using np.load and before you use np.save to save your file object.

Upvotes: 2

Related Questions