Welcome folks today in this blog post we will be removing
or deleting a directory
inside python using pathlib
library. All the full source code of the application is given below.
Get Started
In order to get started you need to make an app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 8 9 10 11 12 |
from pathlib import Path def rmdir(directory): directory = Path(directory) for item in directory.iterdir(): if item.is_dir(): rmdir(item) else: item.unlink() directory.rmdir() rmdir(Path("uploads")) |
So here uploads
is the actual path of the directory it is present inside the root directory
as shown below
Now if you run this python script
by typing the below command as shown below
python app.py
So it will remove that uploads
directory