Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python, remove directory: error File exists

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 !

like image 735
Guillaume Erb Avatar asked Jun 27 '12 14:06

Guillaume Erb


1 Answers

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.

like image 148
Stefano Borini Avatar answered Sep 27 '22 22:09

Stefano Borini