Find all sub-files under a folder
ImportOsallfile=[]defdirlist (path): FileList=os.listdir (path)#FPATH=OS.GETCWD () forFileNameinchfilelist:#allfile.append (fpath+ '/' +filename)Filepath=Os.path.join (path,filename)ifOs.path.isdir (filepath): Dirlist (filepath)Else: Allfile.append (filepath)#Print filepathDirlist ('c:/users/lin/desktop/python/test/a')PrintAllfile
Delete all sub-files under a folder
ImportOSdefRemoveFile (filedir): forNameinchOs.listdir (filedir): filepath=Os.path.join (filedir,name)ifOs.path.isfile (filepath): Os.remove (filepath)if __name__=="__main__": RemoveFile ('c:/users/lin/desktop/python/test/a')
ImportOSImportRedefRemoveFile (filedir): forNameinchOs.listdir (filedir): filepath=Os.path.join (filedir,name)ifOs.path.isfile (filepath): s=r'\d*\.jpg'Reg=Re.compile (s) match=reg.search (name)ifmatch:os.remove (filepath)if __name__=="__main__": RemoveFile ('c:/users/lin/desktop/python/test')#Print K
Python bulk Find and delete file operations