in Python, the Glob module is the
The matching rules in the Unix shell need to be used in the found condition:
* : matches all
? : matches one character
*.* : matches such as: [Hello.txt,cat.xls,xxx234s.doc]
?. *  : matches such as: [1.txt,h.py]
?. GIF: matches such as: [X.gif,2.gif]
If there is no match, Glob.glob (path) will return an empty list:[]
Import Glob def get_all (): "' Get directory [F:\\wfpdm\\my_proc_data_zxtz] All files below ' return Glob.glob (' f:\\ Wfpdm\\my_proc_data_zxtz\\*.* ') def get_my_file (): "' Get directory [F:\\wfpdm\\my_proc_data_zxtz] The following file name is a 4-character file "' return Glob.glob (' f:\\wfpdm\\my_proc_data_zxtz\\????. TXT ') def get_batch_file (): "" Gets the directory [F:\\wfpdm\\my_proc_data_zxtz] under the file "'. txt\ ' with the extension '" ' return Glob.glob ( ' F:\\wfpdm\\my_proc_data_zxtz\\*.txt ') def Main (): print (' Get directory [F:\\wfpdm\\my_proc_data_zxtz] All files below: ') tem_files = Get_all () print (tem_files) print (' Get directory [F:\\wfpdm\\my_proc_data_zxtz] file named 4 characters: ') tem_files = get_my_file () print (tem_files) print (' Get directory [f:\\wfpdm\\my_proc_data_zxtz] under extension '. txt ' \ ' file: ') tem_files = Get_batch_file () print (tem_files) if __name__ = = ' __main__ ': Main ()
Alternatively, you can use the Join method to achieve file acquisition.
From Os.path import exists, Isdir, basename, join, splitextfrom glob import globextensions = ['. Zxtz ']def get_categories (d Atasetpath): "Get all categories, folder name" ' cat_paths = [files for files in glob (Datasetpath + "/*") if Isdir ( files)] cat_paths.sort () cats = [BaseName (Cat_path) for Cat_path in Cat_paths] return catsdef get_files ( Path, extensions=extensions): "returns all video filenames under the path path of the classpath, list ' all_files = [] all_files.extend ([Join] ( Path, basename (fname)), fname in Glob (path + "/*") if Splitext (fname) [1] in extensions]) return All_ Files
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Python Glob Read all folders or file methods under path