Cro*_*roc 7 python contains metacharacters
我正在使用os.walk(目录)递归显示该目录中的所有文件.问题是我只需要在其名称中显示包含问题字符串的文件,并且还必须管理元字符.
我现在拥有的是:
for root, subdirs, files in os.walk(dir1):
for filename in files:
if substring in filename:
name_path = os.path.join(root,filename)
list.insert(END, name_path)
Run Code Online (Sandbox Code Playgroud)
这很好用,但如果 substring = *因为我没有包含'*'的文件,我的列表是空的.
那么,如果substring包含METACHAR ,我该如何使其工作?
我想你正在寻找fnmatch:
https://docs.python.org/3/library/fnmatch.html#module-fnmatch
| 归档时间: |
|
| 查看次数: |
34610 次 |
| 最近记录: |