Closing a file after using np.load (using Spyder)
Asked Answered
T

2

3

I am loading a file:

a= np.load('myfile.npz')

and then doing things with a

After a while I regenerate myfile.npz (on a remote machine).

When I attempt to copy the file across (using WinSCP) from the remote machine it fails, reporting:

System Error.  Code: 32.
The process cannot access the file because it is being used by another process.

I've tried this:

>>> a.fid
<open file 'myfile.npz', mode 'rb' at 0x058A78B8>
>>> a.fid.close()
>>> a.fid
<closed file 'myfile.npz', mode 'rb' at 0x058A78B8>

However, the file copy still fails.

If I close the python interpreter, the copy succeeds.

What is causing this porblem? Do I need to close myfile.npz (I had thought this was handled automatically) first? If so, how do I do it?

I'm using a python console with in the Spyder IDE on Win7.

Tiffaneytiffani answered 26/9, 2014 at 16:22 Comment(0)
T
4

I have found that using del also works:

del a
Tiffaneytiffani answered 26/9, 2014 at 17:28 Comment(0)
M
13

Try using the with context manager:

with np.load('myfile.npz') as a:
    do_stuff(a)

do_morestuff() # a is closed now

Context managers automatically take care of closing the resource once you are done with it.

Mewl answered 26/9, 2014 at 16:40 Comment(1)
Thanks, that works but I'm playing around with a on the command line. The tabs required before each command are irksome (as I'm copying previous commands as well).Tiffaneytiffani
T
4

I have found that using del also works:

del a
Tiffaneytiffani answered 26/9, 2014 at 17:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.