I need to delete folder where I have the script stored and executed from. Ex.:
Folder:
- script.py
- some_other_content
And I need to delete the Folder. I tried using combination of shutil.rmtree()
and os.rmdir()
like this:
import shutil, os
path = os.path.abspath(__file__ + "/../") #points to Folder
shutil.rmtree(path)
os.rmdir(path)
But the script got deleted after shutil.rmtree(path)
executes and gets removed and thus got terminated before actually removing the Folder (but still removes content of it). And if I swap the order of lines I get OSError
.
Any idea?
Edit: What happens to script when i delete it in middle of execution?
System info: Windows 10, Python 3.7