The glob module is one of the simplest modules with very little content. Use it to find file pathnames that match specific rules. It is similar to using file search under Windows. Only three matching characters are used to find files: "*", "?", "[]". "*" matches 0 or more characters; "?" matches a single character; "[]" matches characters within the specified range, such as: [0-9] matches numbers.
glob.glob
Returns a list of all matching file paths. It has only one parameter pathname, which defines the file path matching rules. It can be an absolute path or a relative path. Here is an example using glob.glob:
import glob #获取指定目录下的所有图片 print glob.glob(r"E:/Picture/*/*.jpg") #获取上级目录的所有.py文件 print glob.glob(r'../*.py') #相对路径
Get a calendarable object, which can be used to obtain matching file path names one by one. The difference with glob.glob() is that glob.glob obtains all matching paths at the same time, while glob.iglob only obtains one matching path at a time. This is somewhat similar to the DataSet and DataReader used in .NET to operate databases. Here is a simple example:
import glob #父目录中的.py文件 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?