How to use shutil.rmtree without specifying a path

621 views Asked by At

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.

2

There are 2 answers

0
WhatsThePoint On BEST ANSWER

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.

0
Henry Heath On

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)