''' Created on Feb 6, 2013 @author: changxue @summary: to delete all files and directories in specified directory. print the name of file or directory if it can't remove. ''' import os, sys def clean(path): # to deal with abnormal situation if not os.path.exists(path): raise Exception('Error: can not find path: %s\n' % path) elif os.path.isfile(path): os.remove(path) return for root, dirs, files in os.walk(path): for f in files: try: full_path = os.path.join(root, f) os.remove(full_path) except: print "Fail remove file: %s\n" % full_path continue for d in dirs: try: os.removedirs(d) except: print "Fail remove: %s\n" % d continue if __name__ == '__main__': usage = 'cleanUp.py dirname' argv_len = len(sys.argv) if argv_len <= 1: print usage else: dest_path = sys.argv[1] clean(dest_path)