新聞中心
glob —- Unix 風(fēng)格路徑名模式擴(kuò)展
源代碼:Lib/glob.py

glob 模塊會(huì)按照 Unix shell 所使用的規(guī)則找出所有匹配特定模式的路徑名稱,但返回結(jié)果的順序是不確定的。 波浪號(hào)擴(kuò)展不會(huì)生效,但 *, ? 以及用 [] 表示的字符范圍將被正確地匹配。 這是通過配合使用 os.scandir() 和 fnmatch.fnmatch() 函數(shù)來實(shí)現(xiàn)的,而不是通過實(shí)際發(fā)起調(diào)用子 shell。
請(qǐng)注意以點(diǎn)號(hào) (.) 打頭的文件只能用同樣以點(diǎn)號(hào)打頭的模式來匹配,這不同于 fnmatch.fnmatch() 或 pathlib.Path.glob()。 (對(duì)于波浪號(hào)和 shell 變量擴(kuò)展,請(qǐng)使用 os.path.expanduser() 和 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 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.sep 或 os.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_dir 和 dir_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_dir 和 dir_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ò)展
分享文章:創(chuàng)新互聯(lián)Python教程:glob—-Unix風(fēng)格路徑名模式擴(kuò)展
網(wǎng)頁鏈接:http://www.dlmjj.cn/article/dpshjdo.html


咨詢
建站咨詢
