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 !
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With