Skip to content

Instantly share code, notes, and snippets.

@rcook
Created February 18, 2022 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rcook/459f7e9b1c7a42792c3f63cb396f4342 to your computer and use it in GitHub Desktop.
Save rcook/459f7e9b1c7a42792c3f63cb396f4342 to your computer and use it in GitHub Desktop.
prune_empty_dirs.py
def prune_empty_dirs(dir, dry_run=True):
for current_dir, ds, _ in os.walk(dir, topdown=False):
for d in ds:
p = os.path.join(current_dir, d)
if os.path.isdir(p):
try:
if not dry_run:
os.removedirs(p)
except FileNotFoundError:
pass
except OSError as e:
if e.winerror != 145:
raise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment