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 的路徑。本函數(shù)帶有 dir_fd 參數(shù),支持 基于目錄描述符的相對路徑。
如果 recursive 為真值,則模式 "
**" 將匹配目錄中的任何文件以及零個或多個目錄、子目錄和符號鏈接。 如果模式加了一個os.sep或os.altsep則將不匹配文件。If include_hidden is true, "
**" pattern will match hidden directories.引發(fā)一個 審計事件
glob.glob附帶參數(shù)pathname,recursive。引發(fā)一個 審計事件
glob.glob/2,附帶參數(shù)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()相同的結果,但不會實際地同時保存它們。引發(fā)一個 審計事件
glob.glob附帶參數(shù)pathname,recursive。引發(fā)一個 審計事件
glob.glob/2,附帶參數(shù)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 風格文件名(而非路徑)擴展