Yes, you can do this. The script will be loaded into memory when it runs, so it can delete its parent directory (and therefore itself) directly from the script without any issues. Just use shutil.rmtree
rather than os.rmdir
, because os.rmdir
can't remove a directory that isn't empty.
Here's a one-liner that will do it (be careful running this in a directory with stuff you don't want deleted!)
shutil.rmtree(os.path.dirname(os.path.realpath(__file__)))