Deleting folders in python recursively
Try shutil.rmtree
:
import shutil
shutil.rmtree('/path/to/your/dir/')
Here's my pure pathlib
recursive directory unlinker:
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("dir/"))
The default behavior of os.walk()
is to walk from root to leaf. Set topdown=False
in os.walk()
to walk from leaf to root.