glob --- Unix 風(fēng)格路徑名模式擴(kuò)展?

源代碼: 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().)

對(duì)于字面值匹配,請(qǐng)將原字符用方括號(hào)括起來。 例如,'[?]' 將匹配字符 '?'。

參見

pathlib 模塊提供高級(jí)路徑對(duì)象。

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,則它應(yīng)當(dāng)是指明要搜索的根目錄的 path-like object。 它用在 glob() 上與在調(diào)用它之前改變當(dāng)前目錄有相同的效果。 如果 pathname 為相對(duì)路徑,結(jié)果將包含相對(duì)于 root_dir 的路徑。

本函數(shù)帶有 dir_fd 參數(shù),支持 基于目錄描述符的相對(duì)路徑。

如果 recursive 為真值,則模式 "**" 將匹配目錄中的任何文件以及零個(gè)或多個(gè)目錄、子目錄和符號(hào)鏈接。 如果模式加了一個(gè) os.sepos.altsep 則將不匹配文件。

If include_hidden is true, "**" pattern will match hidden directories.

引發(fā)一個(gè) 審計(jì)事件 glob.glob 附帶參數(shù) pathname, recursive。

引發(fā)一個(gè) 審計(jì)事件 glob.glob/2,附帶參數(shù) pathname, recursive, root_dir, dir_fd

備注

在一個(gè)較大的目錄樹中使用 "**" 模式可能會(huì)消耗非常多的時(shí)間。

在 3.5 版更改: 支持使用 "**" 的遞歸 glob。

在 3.10 版更改: 添加了 root_dirdir_fd 形參。

在 3.11 版更改: Added the include_hidden parameter.

glob.iglob(pathname, *, root_dir=None, dir_fd=None, recursive=False, include_hidden=False)?

返回一個(gè) iterator,它會(huì)產(chǎn)生與 glob() 相同的結(jié)果,但不會(huì)實(shí)際地同時(shí)保存它們。

引發(fā)一個(gè) 審計(jì)事件 glob.glob 附帶參數(shù) pathname, recursive。

引發(fā)一個(gè) 審計(jì)事件 glob.glob/2,附帶參數(shù) pathname, recursive, root_dir, dir_fd。

在 3.5 版更改: 支持使用 "**" 的遞歸 glob。

在 3.10 版更改: 添加了 root_dirdir_fd 形參。

在 3.11 版更改: Added the include_hidden parameter.

glob.escape(pathname)?

轉(zhuǎn)義所有特殊字符 ('?', '*''[')。 這適用于當(dāng)你想要匹配可能帶有特殊字符的任意字符串字面值的情況。 在 drive/UNC 共享點(diǎn)中的特殊字符不會(huì)被轉(zhuǎn)義,例如在 Windows 上 escape('//?/c:/Quo vadis?.txt') 將返回 '//?/c:/Quo vadis[?].txt'

3.4 新版功能.

例如,考慮一個(gè)包含以下內(nèi)容的目錄:文件 1.gif, 2.txt, card.gif 以及一個(gè)子目錄 sub 其中只包含一個(gè)文件 3.txt. glob() 將產(chǎn)生如下結(jié)果。 請(qǐng)注意路徑的任何開頭部分都將被保留。:

>>>
>>> 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/']

如果目錄包含以 . 打頭的文件,它們默認(rèn)將不會(huì)被匹配。 例如,考慮一個(gè)包含 card.gif.card.gif 的目錄:

>>>
>>> import glob
>>> glob.glob('*.gif')
['card.gif']
>>> glob.glob('.c*')
['.card.gif']

參見

模塊 fnmatch

Shell 風(fēng)格文件名(而非路徑)擴(kuò)展