tempfile — 生成临时文件和目录
tempfile
glob
源代码: Lib/glob.py
The glob 模块根据 Unix Shell 使用规则,查找匹配指定模式的所有路径名,尽管结果以任意次序返回。不展开 ~,但 * , ? ,和字符范围表达采用 [] 将被正确匹配。做到这是通过使用 os.scandir() and fnmatch.fnmatch() 函数一致,而不是通过援引子 Shell。
*
?
[]
os.scandir()
fnmatch.fnmatch()
注意,文件开头采用点 ( . ) 只能匹配也以点开头的模式,不像 fnmatch.fnmatch() or pathlib.Path.glob() 。(为展开 ~ 和 Shell 变量,使用 os.path.expanduser() and os.path.expandvars() )。
.
pathlib.Path.glob()
os.path.expanduser()
os.path.expandvars()
对于文字匹配,将元字符包裹在括号中。例如, '[?]' 匹配字符 '?' .
'[?]'
'?'
另请参阅
The pathlib 模块提供高级路径对象。
pathlib
返回可能为空的路径名列表匹配 pathname ,必须是包含路径规范的字符串。 pathname 可以是绝对的 (像 /usr/src/Python-1.5/Makefile ) 或相对的 (像 ../../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.
/usr/src/Python-1.5/Makefile
../../Tools/*/*.gif
若 root_dir 不是 None ,它应该是 像路径对象 指定要搜索的根目录。它有相同效果对于 glob() 如在调用它之前改变当前目录。若 pathname 是相对的,结果包含的路径将相对于 root_dir .
None
glob()
此函数可以支持 相对于目录描述符的路径 采用 dir_fd 参数。
若 recursive 为 True,模式 ** 将匹配任何文件、零个或多个目录、子目录及到目录的符号链接。若模式之后紧跟 os.sep or os.altsep 那么将不匹配文件。
**
os.sep
os.altsep
若 include_hidden 为 True, ** 模式将匹配隐藏目录。
引发 审计事件 glob.glob 采用自变量 pathname , recursive .
glob.glob
pathname
recursive
引发 审计事件 glob.glob/2 采用自变量 pathname , recursive , root_dir , dir_fd .
glob.glob/2
root_dir
dir_fd
注意
使用 ** 模式在大型目录树中可能消耗过多时间。
This function may return duplicate path names if pathname contains multiple “ ** ” patterns and recursive 为 True。
3.5 版改变: 支持递归 glob 使用 ** 。
3.10 版改变: 添加 root_dir and dir_fd 参数。
3.11 版改变: 添加 include_hidden 参数。
返回 iterator 产生相同值如 glob() 不用同时实际存储它们所有。
转义所有特殊字符 ( '?' , '*' and '[' )。这很有用,若想要匹配其中可能拥有特殊字符的任意文字字符串。不转义在 "驱动器/UNC" 共享点中的特殊字符,如在 Windows escape('//?/c:/Quo vadis?.txt') 返回 '//?/c:/Quo vadis[?].txt' .
'*'
'['
escape('//?/c:/Quo vadis?.txt')
'//?/c:/Quo vadis[?].txt'
Added in version 3.4.
例如,考虑目录包含下列文件: 1.gif , 2.txt , card.gif 和子目录 sub 仅包含文件 3.txt . glob() 将产生下列结果。预告,如何预留路径的任何前导分量。
1.gif
2.txt
card.gif
sub
3.txt
>>> 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 and .card.gif :
.card.gif
>>> import glob >>> glob.glob('*.gif') ['card.gif'] >>> glob.glob('.c*') ['.card.gif']
fnmatch
Shell 样式文件名 (非路径) 扩展
fnmatch — Unix 文件名模式匹配
键入搜索术语或模块、类、函数名称。