官方解释如下:
https://docs.python.org/2/library/glob.html
The glob module finds all the pathnames matching a specified pattern according to the rules used by the Unix shell, although results are returned in arbitrary order. No tilde expansion is done, but *, ?, and character ranges expressed with [] will be correctly matched. This is done by using the os.listdir() and fnmatch.fnmatch() functions in concert, and not by actually invoking a subshell. Note that unlike fnmatch.fnmatch(), glob treats filenames beginning with a dot (.) as special cases. (For tilde and shell variable expansion, use os.path.expanduser() and os.path.expandvars().)
翻一下大概是这个意思:
glob模块查找匹配特定表达式的所有路径名,哪怕结果返回任意次,Unix shell中的查找规则与表达式规则一样。不支持波形扩展符号,仅使用“*”和“?”,如果表达式使用“[]”就不行完全匹配。这是通过一起使用os.listdir()和fnmatch.fnmatch()函数来完成的,而不是通过实际调用子shell来完成的。 请注意,与fnmatch.fnmatch()不同,glob将以点(.)开头的文件名视为特殊情况。 (对于波形和shell变量扩展,请使用os.path.expanduser()和os.path.expandvars()。)
”*”匹配0个或多个字符;”?”匹配单个字符;”[]”匹配指定范围内的字符,如:[0-9]匹配数字。
For a literal match, wrap the meta-characters in brackets. For example, ‘[?]’ matches the character ‘?’.
对于文字匹配,将元字符括在括号中。 例如,’[?]’匹配字符’?’。
glob.glob(pathname)
Return a possibly-empty list of path names that match pathname, which must be a string containing a path specification. pathname can be either absolute (like /usr/src/Python-1.5/Makefile) or relative (like ../../Tools//.gif), and can contain shell-style wildcards. Broken symlinks are included in the results (as in the shell).
返回匹配路径名的可能为空的路径名列表,该列表必须是包含路径规范的字符串。 路径名可以是绝对的(如/usr/src/Python-1.5/Makefile)或相对的(如../../Tools//.gif),并且可以包含shell风格的通配符。 结果中包含损坏的符号链接(如在shell中)。
glob.iglob(pathname)
Return an iterator which yields the same values as glob() without actually storing them all simultaneously.
返回一个产生与glob()相同的值的迭代器,而不实际同时全部存储它们。
For example, consider a directory containing only the following files: 1.gif, 2.txt, and card.gif. glob() will produce the following results. Notice how any leading components of the path are preserved.
例如,考虑只包含以下文件的目录:1.gif,2.txt和card.gif。 glob()将产生以下结果。 注意如何保留路径的任何主要组件。
>>> import glob
>>> glob.glob('./[0-9].*')
['./1.gif', './2.txt']
>>> glob.glob('*.gif')
['1.gif', 'card.gif']
>>> glob.glob('?.gif')
['1.gif']
If the directory contains files starting with . they won’t be matched by default. For example, consider a directory containing card.gif and .card.gif:
如果目录中包含以”.”开头的文件。 他们将不会被默认匹配。 例如,考虑一个包含card.gif和.card.gif的目录:
>>> import glob
>>> glob.glob('*.gif')
['card.gif']
>>> glob.glob('.c*')
['.card.gif']
我自己的例子
print glob.glob(r"C:/Users/Public/*/*/*.jpg")
f = glob.iglob(r'*.py')#当前目录所有py文件,与glob区别,iglob每次只获取一个匹配路径
for py in f:
print py
运行结果如下:
['C:/Users/Public/Pictures\\Sample Pictures\\Chrysanthemum.jpg', 'C:/Users/Public/Pictures\\Sample Pictures\\Desert.jpg', 'C:/Users/Public/Pictures\\Sample Pictures\\Hydrangeas.jpg', 'C:/Users/Public/Pictures\\Sample Pictures\\Jellyfish.jpg', 'C:/Users/Public/Pictures\\Sample Pictures\\Koala.jpg', 'C:/Users/Public/Pictures\\Sample Pictures\\Lighthouse.jpg', 'C:/Users/Public/Pictures\\Sample Pictures\\Penguins.jpg', 'C:/Users/Public/Pictures\\Sample Pictures\\Tulips.jpg']
imagestudy.py
login-variables.py
LyhLibrary.py
mylibrary.py
practice.py
Test.py
zhlibrary.py
__init__.py
__init__mylibrary.py