CommunityScripts
CommunityScripts copied to clipboard
[renamerOnUpdate] Remove empty parent folders
This could be made to remove all empty parent folders instead of just the current one.
https://github.com/stashapp/CommunityScripts/blob/468e9df869de39121acbb875abadf9785c627d1a/plugins/renamerOnUpdate/renamerOnUpdate.py#L1174-L1183
def remove_empty_parent_folders(path):
# Walk the directory tree in reverse order (bottom-up)
for root, dirs, files in os.walk(path, topdown=False):
# If the directory is empty, try to remove it
if not files and not dirs:
try:
os.rmdir(root)
print(f"Removed empty directory: {root}")
except OSError as e:
print(f"Failed to remove directory {root}: {e}")
if REMOVE_EMPTY_PARENT_FOLDERS:
remove_empty_parent_folders(current_dir)