glob --- Unix 风格路径名模式扩展¶
源代码: Lib/glob.py
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.scandir() and fnmatch.fnmatch() functions in concert, and
not by actually invoking a subshell.
Note that files beginning with a dot (.) can only be matched by
patterns that also start with a dot,
unlike fnmatch.fnmatch() or pathlib.Path.glob().
(For tilde and shell variable expansion, use os.path.expanduser() and
os.path.expandvars().)
对于字面值匹配,请将原字符用方括号括起来。 例如,'[?]' 将匹配字符 '?'。
参见
pathlib 模块提供高级路径对象。
- glob.glob(pathname, *, root_dir=None, dir_fd=None, recursive=False, include_hidden=False)¶
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). Whether or not the results are sorted depends on the file system. If a file that satisfies conditions is removed or added during the call of this function, whether a path name for that file will be included is unspecified.如果 root_dir 不为
None,则它应当是指明要搜索的根目录的 path-like object。 它用在glob()上与在调用它之前改变当前目录有相同的效果。 如果 pathname 为相对路径,结果将包含相对于 root_dir 的路径。本函数带有 dir_fd 参数,支持 基于目录描述符的相对路径。
如果 recursive 为真值,则模式 "
**" 将匹配目录中的任何文件以及零个或多个目录、子目录和符号链接。 如果模式加了一个os.sep或os.altsep则将不匹配文件。If include_hidden is true, "
**" pattern will match hidden directories.引发一个 审计事件
glob.glob附带参数pathname,recursive。引发一个 审计事件
glob.glob/2,附带参数pathname,recursive,root_dir,dir_fd。备注
在一个较大的目录树中使用 "
**" 模式可能会消耗非常多的时间。在 3.5 版更改: 支持使用 "
**" 的递归 glob。在 3.10 版更改: 添加了 root_dir 和 dir_fd 形参。
在 3.11 版更改: Added the include_hidden parameter.
- glob.iglob(pathname, *, root_dir=None, dir_fd=None, recursive=False, include_hidden=False)¶
返回一个 iterator,它会产生与
glob()相同的结果,但不会实际地同时保存它们。引发一个 审计事件
glob.glob附带参数pathname,recursive。引发一个 审计事件
glob.glob/2,附带参数pathname,recursive,root_dir,dir_fd。在 3.5 版更改: 支持使用 "
**" 的递归 glob。在 3.10 版更改: 添加了 root_dir 和 dir_fd 形参。
在 3.11 版更改: Added the include_hidden parameter.
- glob.escape(pathname)¶
转义所有特殊字符 (
'?','*'和'[')。 这适用于当你想要匹配可能带有特殊字符的任意字符串字面值的情况。 在 drive/UNC 共享点中的特殊字符不会被转义,例如在 Windows 上escape('//?/c:/Quo vadis?.txt')将返回'//?/c:/Quo vadis[?].txt'。3.4 新版功能.
例如,考虑一个包含以下内容的目录:文件 1.gif, 2.txt, card.gif 以及一个子目录 sub 其中只包含一个文件 3.txt. glob() 将产生如下结果。 请注意路径的任何开头部分都将被保留。:
>>> import glob
>>> glob.glob('./[0-9].*')
['./1.gif', './2.txt']
>>> glob.glob('*.gif')
['1.gif', 'card.gif']
>>> glob.glob('?.gif')
['1.gif']
>>> glob.glob('**/*.txt', recursive=True)
['2.txt', 'sub/3.txt']
>>> glob.glob('./**/', recursive=True)
['./', './sub/']
如果目录包含以 . 打头的文件,它们默认将不会被匹配。 例如,考虑一个包含 card.gif 和 .card.gif 的目录:
>>> import glob
>>> glob.glob('*.gif')
['card.gif']
>>> glob.glob('.c*')
['.card.gif']
参见
- 模块
fnmatch Shell 风格文件名(而非路径)扩展