Sunday, July 5, 2009

Recursively removing empty directories using python


#!/usr/bin/python
import os
from os.path import join
def remEmptyDir(mypath):
for root, dirs, files in os.walk(mypath,topdown=False):
for name in dirs:
fname = join(root,name)
if not os.listdir(fname): #to check wither the dir is empty
print fname
os.removedirs(fname)

remEmptyDir('/home/suresh/photos')