I need to develop a script that will run some calculations. You want this script to handle ^ C correctly by deleting the temporary directory. I tried several versions of the code in signal_handler:
shutil.rmtree(self.temp)
or even
os.system("rm -rf " + self.temp)
when I interrupt the execution and the handler is called to delete the directory, I get errors such as:
OSError: [Errno 17] File exists : 'foo'
or
rm: Unable to remove directory foo: File exists
After execution, the directory that I want to delete is empty, and I can delete it using rm -rthe 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)
Of course, I get errors, but the second loop finds this file: .nfsA13D3
Does anyone have a solution to my problem? Thank!
source
share