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 !
.nfs*
files get created when yourm
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