In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how python uses glob to retrieve files". In daily operation, I believe many people have doubts about how python uses glob to retrieve documents. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how python uses glob to retrieve files". Next, please follow the editor to study!
1. Retrieve all files and folders under the current directory from glob import globglob ('. / *') Output: ['. / 0a.folders,'. / aaa','. / 1b.folders,'. / 1a.files,'. / 0b.folders'] # return a list2. Retrieve the files with the specified suffix name under the current directory
Suppose we need to get all the files under the current folder with the suffix ".wav".
From glob import globglob ('. / * .wav') Output: ['. / 0a.characters,'. / 1b.characters,'. / 1a.records,'. / 0b.paths] # returns a list containing the path to the ".wav" file. II. Retrieve files containing specified characters in the current directory.
Suppose we need to get all the ".wav" files that contain the "a" character
From glob import globglob ('. / * axi.room.room') Output: ['. / 0a.resume,'. / 1a.room'] # returns a list that contains the path to the ".wav" file
Add: the glob module in Python uses notes (it's really convenient to use it to traverse folders and files)
Introduction to glob Modul
Glob is a file operation module brought by python itself, which can be used to find files in line with their own purposes, similar to the file search under Windows, supporting wildcard operations.
There are three types of wildcards supported in glob:
'*' matches 0 or more characters
'?' Match only one character
'[]' matches characters within a specified range, such as [0-9] matching numbers
1. Use of glob.glob:
This method returns a list of all matching file paths.
This method requires a parameter to specify the file path (string type) absolute path and relative path to match.
1. Matches a file with a suffix of .exe under a specified path (absolute path) In:from glob import globfile = glob (ringing Dmasmies. Exe') print (type (file)) print (file) Out: ['D:\ masm\ CREF.EXE','D:\ masm\ debug.exe', 'D:\ masm\ DEBUG32.EXE', 'D:\ masm\ ERROUT.EXE', 'D:\ masm\ EXEMOD.EXE','D:\ masm\ EXEPACK.EXE' 'D:\ masm\ LIB.EXE', 'D:\ masm\ LINK.EXE', 'D:\ masm\ MASM.EXE', 'D:\ masm\ SETENV.EXE', 'D:\ masm\ TEST2.EXE', 'D:\ masm\ TEST3.EXE', 'D:\ masm\ TEST4.EXE', 'D:\ masm\ TEST5.EXE', 'D:\ masm\ TEST6.EXE'] 2. Match all files under a specified path (absolute path) In:from glob import globfile = glob (ringing Dlaze masmages') print (type (file)) print (file) Out: ['D:\ masm\ work\ hello.asm', 'D:\ masm\ work\ HELLO.OBJ', 'D:\ masm\ work\ test1.asm', 'D:\ masm\ work\ TEST1.OBJ', D:\ masm\ work\ test2.asm' 'd:\ masm\ work\ TEST2.OBJ', 'D:\ masm\ work\ test3.asm','D:\ masm\ work\ TEST3.OBJ','D:\ masm\ work\ test4.asm', 'D:\ masm\ work\ TEST4.OBJ', 'D:\ masm\ work\ two'] 3. Use relative paths to match
'.' Refer to the current directory
Play. . 'refers to the directory one level above the current directory
In:from glob import globfile = glob (ringing.) print (type (file)) print (file) Out: ['..\ 12306','..\ Auto connection school network','..\ cache','..\ class','..\ exercise_1.py','..\ exercise_2-2.pyrogen,'..\ exercise_2-3.pyrogen,'..\ exercise_2.py','..\ regular','..\ taobao' '..\ test.py','..\ testglob','..\ UDPClient']
II. The use of glob.iglob
Gets a generator object that you can use to get matching file pathnames one by one. The difference with glob.glob () is that glob.glob gets all the matching paths at the same time, while glob.iglob only gets one matching path at a time.
In:from glob import iglobfile = iglob (ringing. Py') print (type (file)) print (file) n=0for i in file: n=n+1 print (str (n), I) Out:1.. exercise_1.py2.. exercise_2-2.py3.. exercise_2-3.py4.. exercise_2.py5. Est.py
As you can see from the for loop in the example, the generator object gives the files under the path to match in turn.
At this point, the study on "how python uses glob to retrieve files" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.