How to delete recursively empty folders in Python3?
os.walk
accepts optional topdown
parameter (default: True).
By providing topdown=False
, you can iterative from child directories first.
def remove_empty_dirs(path):
for root, dirnames, filenames in os.walk(path, topdown=False):
for dirname in dirnames:
remove_empty_dir(os.path.realpath(os.path.join(root, dirname)))