Date Tags Python

Late one night I needed to write some Python code to recursively find empty directories. Searching online produced some unsatisfactory solutions. Here is a simple solution that leverages os.walk.

import os

empty_dirs = []
for root, dirs, files in os.walk(starting_path):
   if not len(dirs) and not len(files):
       empty_dirs.append(root)

If you then wish to delete these empty directories:

for path in empty_dirs:
    os.removedirs(path)

Comments

comments powered by Disqus