python - Python fnmatch 带括号和逗号
问题描述
我正在浏览一个目录,并尝试(0, 0, 0)
使用fnmatch.fnmatch(filename,'*(0, 0, 0)*')
. 看起来括号和逗号正在抛弃它,并与(0, 1, 1)
我不想要的字符串匹配。
相关的片段是:
for root, dirs, files in os.walk(data_dir):
for file in files:
filename = os.path.join(root, file)
if fnmatch.fnmatch(filename,'*\(0, 0, 0\)*'):
# do stuff
并且目录包含以下文件:
\c_(0, 0, 0)\data.txt
\c_(0, 05, 05)\data.txt
\c_(0, 05, 1)\data.txt
\c_(0, 1, 0)\data.txt
我的理解是转义括号应该可以解决它,但那里没有运气。解决此问题的最佳方法是什么?
解决方案
files_list=[]
for filename in os.listdir(r'DirPath'):
if filename.count('(0, 0, 0)'):
files_list.append(filename)
推荐阅读
- mysql - docker-compose 内的 django-mysql 连接拒绝连接
- mysql - 加入 3 个表向上计算一个表中的最高用户
- javascript - Highchart 折线图工具提示显示索引值而不是 X 轴格式的标签名称
- haskell - Haskell,对 Int 列表进行排序
- postgresql - 我的 postgresql 查询有问题
- html - 由于向描述中添加图标按钮,在未对齐后垂直对齐 mat-expansion-panel 标题
- c# - 从概念上比较 C# String 和 StringBuilders
- angular - Take 不是函数:Angular 7 | Observable.take 抛出运行时错误
- three.js - 三个 JS 轨道控制缩放、平移和移动监听器
- mysql - MySql 5.7 函数 UUID() 默认排序规则 - 排序规则的非法混合