Delete empty directories in the specified directory tree #! /usr/bin env pythonimport osimport sysdir =sys.argv[1]if os.path.isdir (dir): For Root,dirs,files in Os.walk (dir,topdown= False): #从最里向外遍历 for D in Dirs:if not Os.listdir (Os.path.join (root,d)): #判断目录是否为空 Os.rmdir (Os.path. Join (root,d)) Else:print '%s is not dir '%dir
The use of the Os.walk () function
For Root,dirs,files in Os.walk (Path,topdown=true,onerro=none)
Returns a ternary tuple, root for each traversed path, DIRS returns a list of directories, and files returns a file list
Topdown default (True) from outside, False to from inside Out
OS Module Common items
Os.listdir (PATH)
OS.GETCWD ()
Os.chdir (PATH)
Os.rmdir (PATH)
Os.remove ()
Os.path.split (PATH)
Os.path.join (Path,filename)
Os.path.isdir ()
Os.path.isfile ()
Os.path.exists ()
Os.path.getsize ()
Os.path.basename ()
Os.path.dirname ()
Os.sep
Python Learning Notes OS module common items