From 1bf11b0414a7fcf785c792b98f6231821bdad4d4 Mon Sep 17 00:00:00 2001 From: Rhet Turnbull Date: Tue, 6 Jul 2021 21:57:43 -0700 Subject: [PATCH] Fixed cleanup to delete empty folders, #491 --- osxphotos/_version.py | 2 +- osxphotos/cli.py | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/osxphotos/_version.py b/osxphotos/_version.py index f9019e95..dd89debb 100644 --- a/osxphotos/_version.py +++ b/osxphotos/_version.py @@ -1,3 +1,3 @@ """ version info """ -__version__ = "0.42.59" +__version__ = "0.42.60" diff --git a/osxphotos/cli.py b/osxphotos/cli.py index 00de9a88..3d688000 100644 --- a/osxphotos/cli.py +++ b/osxphotos/cli.py @@ -3280,13 +3280,13 @@ def cleanup_files(dest_path, files_to_keep, fileutil): # delete empty directories deleted_dirs = [] - for p in pathlib.Path(dest_path).rglob("*"): - path = str(p).lower() - # if directory and directory is empty - if p.is_dir() and not next(p.iterdir(), False): - verbose_(f"Deleting empty directory {p}") - fileutil.rmdir(p) - deleted_dirs.append(str(p)) + # walk directory tree bottom up and verify contents are empty + for dirpath, _, _ in os.walk(dest_path, topdown=False): + if not list(pathlib.Path(dirpath).glob("*")): + # directory and directory is empty + verbose_(f"Deleting empty directory {dirpath}") + fileutil.rmdir(dirpath) + deleted_dirs.append(str(dirpath)) return (deleted_files, deleted_dirs)