python-3.x - Python中区分大小写的文件路径匹配
问题描述
我有一个带有此路径的目录
Al Furjan/Al Furjan 2D 3D/3D/AZIZI tulip/file.jpg
我有另一个 csv 从中创建路径,路径变成这样
Al Furjan/Al Furjan 2D 3D/3D/AZIZI Tulip/file.jpg
我生成的路径有T大写,但实际目录有小 t。我如何用 python 解决这个问题,它在搜索路径时忽略了区分大小写。
解决方案
您可以使用fnmatch
, re
and的连词os
来做到这一点:
import os
import fnmatch
import re
# translate the file name in a pattern
regex = fnmatch.translate('testit/testIT/testIT.txt')
# compile a case insensitive version of your this pattern
rec = re.compile(regex, re.IGNORECASE)
def listallfiles(path):
for (dirpath, dirnames, filenames) in os.walk(path):
for f in filenames:
yield(os.path.relpath(os.path.join(dirpath, f)))
found = [i for i in listallfiles('.') if rec.match(i)]
print(found)
推荐阅读
- asp.net-core - MailKit.Security.SslHandshakeException:主机名与服务器 SSL 证书中给出的名称不匹配。asp.net 核心 5,nginx
- reactjs - 如何编写一个依赖于反应道具的计时器间隔?
- python - 如何获得 catboost 可视化以显示类别
- python - AttributeError:“张量”对象在 GPflow 中没有属性“ndim”
- maxima - wxMaxima: 将字符串代入函数会创建什么样的对象?
- java - 如何让 GRPC 的 round_robin 负载平衡在 Kubernetes 集群中使用 grpc-java 工作?
- junit - 如何在 Mockito.Mock() 之后注入 Mock
- r - 如何从R中的文本语料库中删除特定单词?提供修改代码
- python - tf.math.reduce_mean 和 tf.keras.metrics.Mean 有什么区别?
- java - 如何摆脱父/子服务的循环依赖?