Created
February 18, 2022 20:28
-
-
Save rcook/459f7e9b1c7a42792c3f63cb396f4342 to your computer and use it in GitHub Desktop.
prune_empty_dirs.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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