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; "?" Matches 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:
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:
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?