To detect and delete empty folders, here’s a Python script that recursively scans a directory and removes all empty folders:
How It Works:
-
Uses
os.walkwithtopdown=Falseto ensure it processes subfolders before their parents. -
Checks if both
dirnamesandfilenamesare empty, meaning the folder is empty. -
Uses
os.rmdir()to delete the folder.
Notes:
-
Only deletes truly empty folders (no files or subfolders inside).
-
Update
target_directoryto your actual path.
Let me know if you need a version with a GUI, a CLI interface, or to run on specific platforms like Windows only.
Share this Page your favorite way: Click any app below to share.