This article mainly introduces in Python uses the Glob module to look for the file path the method, uses the module the Iglob method to realize is very simple, needs the friend to be possible to refer to under
The Glob module is one of the simplest modules with very little content. Use it to find a file path name that matches a specific rule. It's about the same as using a file search under Windows. The lookup file uses only three matches: "*", "?", "[]". "*" matches 0 or more characters; "?" Match a single character; [] matches a specified range of characters, such as: [0-9] matches a number.
Glob.glob
Returns a list of all matching file paths. It has only one parameter pathname, defines the file path matching rule, here can be absolute path, also can be a relative path. The following are examples of using Glob.glob:
?
1 2 3 4 5 6 7 8 |
Import glob #获取指定目录下的所有图片 print Glob.glob (r "e:/picture/*/*.jpg") #获取上级目录的所有. py File Print Glob.glob (r). /*.py ') #相对路径 |
Glob.iglob
Gets a calendar object that can be used to get a matching file pathname. The difference from Glob.glob () is that Glob.glob gets all the matching paths at the same time, and Glob.iglob gets only one matching path at a time. It's kind of like. NET, the dataset and DataReader used in the Operation database. The following is a simple example:
?
1 2 3 4 5 6 7 8 9 10 |
Import glob #父目录中的. py file F = Glob.iglob (R ' ... /*.py ') Print F #<generator object Iglob at 0x00b9ff80> for py in f:print py |
It ' s so easy, is ' t it?