Python, remove directory: error File exists
Asked Answered
A

2

7

I need to develop a script that will launch some computations. A want this script to handle ^C correctly by deleting some temporary directory. I have tried several versions of code in the signal_handler:

shutil.rmtree(self.temp)

or even

os.system("rm -rf " + self.temp)

when I am interrupting the execution and the handler is called to remove the directory, I am getting errors like :

OSError: [Errno 17] File exists : 'foo' 

or

rm: Unable to remove directory foo: File exists

After execution, the directory I want to delete is empty, and I can delete it with a rm -r in the shell. However, if I execute the code :

for f  in os.listdir(self.temp):
    os.remove(os.path.join(self.temp,f))

for f in os.listdir(self.temp):
    print f

os.rmdir(self.temp)

I am, of course, getting errors, but the second loop finds this file: .nfsA13D3

Anyone have a solution to my problem ? Thank you !

Alessandro answered 27/6, 2012 at 14:6 Comment(1)
.nfs* files get created when you rm a file on an NFS mount that is still in use by something. Once the reference count on the .nfs file drops to 0, it should disappear (I have run into cases in the past where it doesn't always, though, requiring manual cleanup).Weingarten
V
10

This is a well known problem with nfs mounted filesystems and some of your utilities are not closing files. An operating system can keep the file alive even if you remove it, but this is not possible when nfs is involved. The solution for the os is to create that temporary .nfs file and keep it around until the file descriptor is in use.

There's no real solution for this problem. The .nfs file will disappear when the last descriptor is closed, but the (empty) directory will still be around. The only possible fix is to find the still open file descriptor and close it, but it depends if it's in your program. In my case, it was in an external, compiled library and I had no chance to find where it leaked.

Vichy answered 28/6, 2012 at 7:22 Comment(0)
A
0

Thank you for the comment, I was opening files in my python code and my signal_handler didn't check if my file objects were closed or not. This is why I wasn't able to delete the directory containing these files in the script.

Alessandro answered 28/6, 2012 at 6:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.