[英]Why python glob.glob does not give me the files I want with the regex I passed in?
例如:
20190108JPYUSDabced.csv
20190107JPYUSDabced.csv
20190106JPYUSDabced.csv
当我从终端搜索前两个文件时:
bash: ls /Users/Downloads/201901{08,07}JPYUSDabced.csv
it gives me the first 2 files (exclude 20190106JPYUSDabced.csv)
当我在python中执行以下操作时:
import glob
glob.glob('/Users/Downloads/201901{08,07}JPYUSDabced.csv')
it gives me []
根据glob
模块的文档,引擎盖glob
使用fnmatch.fnmatch
。 fnmatch
文档描述的唯一模式是:
Pattern | Meaning --------- | ----------------------------- * | matches everything ? | matches any single character [seq] | matches any character in seq [!seq] | matches any character not in seq
对于文字匹配,请将元字符括在方括号中。 例如,“ [?]”与字符“?”匹配。
尝试在方括号中使用字符序列:
glob.glob('/Users/Downloads/2019010[87]JPYUSDabced.csv')
假设您要搜索特定的日期范围,则可能需要尝试使用带有re
表达式的os.walk
来获取要查找的更复杂的模式。
警告: os.walk
从起始位置递归遍历每个目录,这可能不是您想要的。
无论您遇到什么情况,都必须调整正则表达式,但这是一个示例:
正则表达式匹配日期20181208
或日期20190107
但必须包含标识符JPYUSDabced.csv
。
regex = re.compile("(?:(?:20181208)|(?:20190107))JPYUSDabced.csv")
files = []
for dirpath, dirnames, filenames in os.walk('/Users/Downloads'):
for f in filenames:
if regex.match(f):
files.append(os.path.join(dirpath, f))
print(files)
# ['/Users/Downloads/20190107JPYUSDabced.csv', '/Users/Downloads/20181208JPYUSDabced.csv']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.