0

How can I get shutil.rmtree() to run on the folder it is in? I know I can just give it the path but I want it to be dynamic so I can move it across folders and it will still run without having to edit the path.

I haven't tried anything because I don't know what to try.

  • Do you mean the current working directory or the directory the current file is in? See http://stackoverflow.com/questions/3430372/how-to-get-full-path-of-current-files-directory-in-python to get the path dynamically – Henry Heath Dec 14 '16 at 15:15
  • thanks, and i meant the directory the current file is in –  Dec 14 '16 at 15:17
  • 1
    can you elaborate on the purpose of this code? looks like a malware to me... – Jean-François Fabre Dec 14 '16 at 15:22
  • to save time when i need to delete a load of things. like most programs? –  Dec 15 '16 at 13:29

2 Answers2

0

Get the path of the directory the current file is in dynamically. Note this will delete the current file too!

import os
import shutil

path = os.path.dirname(os.path.abspath(__file__))
shutil.rmtree(path)
Henry Heath
  • 1,072
  • 11
  • 19
0

The simplest solution would be just these two lines of code.

import shutil
shutil.rmtree('./')

It deletes the content of the current folder, and itself.

Note. Be careful with this, as with my experience running this, the files do not appear in the recycle bin.

I cannot be liable for any unwanted loss of files.

WhatsThePoint
  • 3,395
  • 8
  • 31
  • 53