I try to delete a created directory in my destructor:
shutil.rmtree("C:\\projects\\project_alpha\\tmp")
It does not work with my python script but when I execute this command via python console it works and the tmp-directory will deleted.
Where is the difference?
I assume by “destructor” you mean the
__del__method.From the docs on del
What you might want to do is register an atexit handler.
For example at module level:
Functions registered with atexit will be run when the interpreter exits regardless of how the interpreter exits.
Of course, you could also do something hacky like force the garbage collector to run (
import gc; gc.collect(), which may force your del method to run but I’m going to go out on a limb here and say that’s a bad idea.😉