All files in python Traverse Folder

'' 'Using the method of walk recursively traverse the directory file, the method returns a walk triples, are root, dirs and files. 
Where the root directory is currently being traversed path; dirs is a list containing the names of all the subdirectories under the current directory traversal are not included in the file directory; 
Files is a list that contains the current directory is traversed file, but does not include subdirectories. 
When the PIL installation: PIP install Pillow '' ' Import os from PIL Import Image DEF ab (path): # traverse the specified folder all the files, check the image size, tall less than deleting 300, not document images are also deleted for root , dirs, in os.walk Files (path): for Files in name: Print (the os.path.join (the root, name)) the try: AA1 = the os.path.join (the root, name) IMG = Image.open ( (aa1)) # image file is not open error, deleting the except: the os.remove (AA1) to delete the file # w = img.width # length and width of the image file h = img.height img.close () if w<300 or h<300: os.remove(aa1) print(aa1) #pass path=r"d:\tuajjb" ab(path) dirs=os.listdir(path) for aa in dirs: #print(os.path.join(path,aa)) #print(aa) pass for root,dirs,files in os.walk(path): for name in dirs: aad="\\" if not os.listdir(path+ aad+ name): print(name) os.removedirs(path+aad+name)#删除文件夹

  

Guess you like

Origin www.cnblogs.com/xkdn/p/12284676.html