0

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

Maneren
  • 98
  • 1
  • 8
  • Did you save your running script in the folder that you want to delete? – YusufUMS Feb 28 '19 at 11:44
  • `os.rmdir` deletes empty directories, like `rmdir` on the command line. That's why you get an `OSError` if you swap the order. `shutil.rmtree` deletes all files and directories recursively. I've tested your script with only the `shutil` call on Ubuntu 16 and it works on my system. However, if you run the script from within `Folder` (i.e. `python script.py`), then your console will not realize that the current working directory was deleted. If you run `cd ../Folder`, it shouldn't be possible to access the folder again. – sauerburger Feb 28 '19 at 12:15

1 Answers1

0

There's no way to delete a whole folder that the script is residing in without deleting the script or moving it.(Don't do that) You should instead leave the script in /bin and then provide command line parameters for the folder you want deleted.

Why do you NEED to have the script reside there? The only other option I can think of is delete all the contents. Answer that and I will ammend by answer to further help.

GRAYgoose124
  • 621
  • 5
  • 24
  • I have folder and i want make script, which when i place it inside a folder, and run it, will delete whole folder including! the script. It will also do some more tasks but they are not important as they don't include that folder. Think about it like about uninstaller for applications – Maneren Mar 06 '19 at 07:30
  • The only viable reason for this is if you are actually packaging it with an application. On windows, I get that it doesn't have as many powerful utilities, but still, I would make a global script that you link in your PATH and just call it command line with the folder location. Are you distributing it as an uninstall tool? Otherwise I can't recommend anything except don't do it, because there are better ways to go about the problem. The easiest solution I can think of is a hack, copy the script out of the folder, launch it from the script in the folder, because otherwise it'll be locked. – GRAYgoose124 Mar 07 '19 at 05:40
  • The only other way I can think of doing it is spawning a process inside the script so that you can let the script finish executing and free up the folder. Another nasty hack, this is why I say it's better to use a cleaner technique. If you're going to do it, just move the script to a temp folder I suppose. This is all a very round-a-bout though. Just because you can do something doesn't mean you should. – GRAYgoose124 Mar 07 '19 at 05:45
  • Yeah i want to use it kind like uninstall. Can you suppose a direct way (I don't think I understand waht you adviced) or piece of code to do this? – Maneren Mar 14 '19 at 21:20