Deleting folders in python recursively Deleting folders in python recursively python python

Deleting folders in python recursively


Try shutil.rmtree:

import shutilshutil.rmtree('/path/to/your/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.


Here's my pure pathlib recursive directory unlinker:

from pathlib import Pathdef rmdir(directory):    directory = Path(directory)    for item in directory.iterdir():        if item.is_dir():            rmdir(item)        else:            item.unlink()    directory.rmdir()rmdir(Path("dir/"))


matomo