How to remove directory with all files in Python?

Member

by emely , in category: Python , 2 years ago

How to remove directory with all files in Python?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by lucile , 2 years ago

@emely You can use shutil.rmtree() method to remove folder and all files inside in Python, below is code example:


1
2
3
4
5
6
import shutil

path = "/tmp/test"

# Remove folder and all files inside
shutil.rmtree(path)


by reynold.dach , a year ago

@emely 

To remove a directory in Python along with all the files and subdirectories it contains, you can use the shutil module which provides a rmtree method that can recursively delete a directory and all its contents. Here is an example:

1
2
3
4
import shutil

directory_path = "/path/to/directory"
shutil.rmtree(directory_path)


This will delete the directory located at directory_path along with all its contents. Make sure to use this with caution as it will permanently delete the directory and its files.