💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
### 导航 - [索引](../genindex.xhtml "总目录") - [模块](../py-modindex.xhtml "Python 模块索引") | - [下一页](fnmatch.xhtml "fnmatch --- Unix filename pattern matching") | - [上一页](tempfile.xhtml "tempfile --- Generate temporary files and directories") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) » - zh\_CN 3.7.3 [文档](../index.xhtml) » - [Python 标准库](index.xhtml) » - [文件和目录访问](filesys.xhtml) » - $('.inline-search').show(0); | # [`glob`](#module-glob "glob: Unix shell style pathname pattern expansion.") --- Unix style pathname pattern expansion **Source code:** [Lib/glob.py](https://github.com/python/cpython/tree/3.7/Lib/glob.py) \[https://github.com/python/cpython/tree/3.7/Lib/glob.py\] - - - - - - The [`glob`](#module-glob "glob: Unix shell style pathname pattern expansion.") 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()`](os.xhtml#os.scandir "os.scandir") and [`fnmatch.fnmatch()`](fnmatch.xhtml#fnmatch.fnmatch "fnmatch.fnmatch") functions in concert, and not by actually invoking a subshell. Note that unlike [`fnmatch.fnmatch()`](fnmatch.xhtml#fnmatch.fnmatch "fnmatch.fnmatch"), [`glob`](#module-glob "glob: Unix shell style pathname pattern expansion.") treats filenames beginning with a dot (`.`) as special cases. (For tilde and shell variable expansion, use [`os.path.expanduser()`](os.path.xhtml#os.path.expanduser "os.path.expanduser") and [`os.path.expandvars()`](os.path.xhtml#os.path.expandvars "os.path.expandvars").) For a literal match, wrap the meta-characters in brackets. For example, `'[?]'` matches the character `'?'`. 参见 The [`pathlib`](pathlib.xhtml#module-pathlib "pathlib: Object-oriented filesystem paths") module offers high-level path objects. `glob.``glob`(*pathname*, *\**, *recursive=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). If *recursive* is true, the pattern "`**`" will match any files and zero or more directories and subdirectories. If the pattern is followed by an `os.sep`, only directories and subdirectories match. 注解 在一个较大的目录树中使用 "`**`" 模式可能会消耗非常多的时间。 在 3.5 版更改: Support for recursive globs using "`**`". `glob.``iglob`(*pathname*, *\**, *recursive=False*)Return an [iterator](../glossary.xhtml#term-iterator) which yields the same values as [`glob()`](#module-glob "glob: Unix shell style pathname pattern expansion.")without actually storing them all simultaneously. `glob.``escape`(*pathname*)Escape all special characters (`'?'`, `'*'` and `'['`). This is useful if you want to match an arbitrary literal string that may have special characters in it. Special characters in drive/UNC sharepoints are not escaped, e.g. on Windows `escape('//?/c:/Quo vadis?.txt')` returns `'//?/c:/Quo vadis[?].txt'`. 3\.4 新版功能. For example, consider a directory containing the following files: `1.gif`, `2.txt`, `card.gif` and a subdirectory `sub`which contains only the file `3.txt`. [`glob()`](#module-glob "glob: Unix shell style pathname pattern expansion.") will produce the following results. Notice how any leading components of the path are preserved. ``` >>> 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/'] ``` 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`: ``` >>> import glob >>> glob.glob('*.gif') ['card.gif'] >>> glob.glob('.c*') ['.card.gif'] ``` 参见 Module [`fnmatch`](fnmatch.xhtml#module-fnmatch "fnmatch: Unix shell style filename pattern matching.")Shell-style filename (not path) expansion ### 导航 - [索引](../genindex.xhtml "总目录") - [模块](../py-modindex.xhtml "Python 模块索引") | - [下一页](fnmatch.xhtml "fnmatch --- Unix filename pattern matching") | - [上一页](tempfile.xhtml "tempfile --- Generate temporary files and directories") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) » - zh\_CN 3.7.3 [文档](../index.xhtml) » - [Python 标准库](index.xhtml) » - [文件和目录访问](filesys.xhtml) » - $('.inline-search').show(0); | © [版权所有](../copyright.xhtml) 2001-2019, Python Software Foundation. Python 软件基金会是一个非盈利组织。 [请捐助。](https://www.python.org/psf/donations/) 最后更新于 5月 21, 2019. [发现了问题](../bugs.xhtml)? 使用[Sphinx](http://sphinx.pocoo.org/)1.8.4 创建。