Python deletes the source code of an empty folder,
Although empty folders do not occupy space, they sometimes seem annoying (don't misunderstand, I'm not obsessive !), So I wrote a small program to delete the empty folder in the current directory.
Environment: win7 64-bit; python2.7; IDE pycharm2016.1
The specific implementation is as follows:
#! /Usr/bin/env python # coding = utf-8import OS, timedef del_emfile (path = OS. getcwd (): ''' parameter: path defaults to the current path ''' folder_num = 0 efile = [] for I in OS. walk (path): if len (I [1]) = 0 and len (I [2]) = 0: efile. append (I [0]) print 'empty Folder \ t', I [0] folder_num + = 1 print 'empty Folder \ t ', folder_num raw_input ('\ n press any key to delete · \ n') efile. reverse () for f in efile: try: # OS. rmdir (f) print 'delete \ t', f then t: print 'Delete \ t', f,' \ t failed 'print '\ n Delete empty folder \ t ', folder_num, '\ n' return folder_numif _ name _ =' _ main _ ': d = 1 while d: d = del_emfile () time. sleep (100)
Result:
This source code may not be perfect. You can use this template for optimization.
Note: the source code is purely original. for academic purposes, please indicate the source. for commercial purposes, please contact me first.