简体   繁体   English

Python glob.glob():在文件名的一部分上查找完全匹配

[英]Python glob.glob(): find exact match on a portion of the file name

I would like to use glob to get all the csv files that have a certain string inside the file name. 我想使用glob来获取文件名中具有特定字符串的所有csv文件。 The string only appears in the middle of the file name. 该字符串仅出现在文件名的中间。 For instance, I would like to extract all csv files that have SI--Exp1 inside the filename, which could look something like 03152018-User1-SI--Exp1-trial14.csv . 例如,我想提取文件名中具有SI--Exp1所有csv文件,看起来像是03152018-User1-SI--Exp1-trial14.csv Here's what I have so far: 这是我到目前为止的内容:

import glob
path =r'C:\YourFolder' #path to folder with .csv files
all = glob.glob(path+"/*'[SI--Exp1]'*.csv")

which I received an error message of bad character range 3-- . 我收到了错误的bad character range 3--的错误消息。 It said from the documentation that square brackets put inside quotation marks help find the literal match. 它从文档中说,方括号放在引号内有助于查找文字匹配。 Any advice? 有什么建议吗?

Apparently the use of quotation marks was unnecessary to find filenames that have a certain string anywhere in the filename. 显然,不必使用引号来查找文件名中任何位置具有特定字符串的文件名。 Here's the solution: 解决方法如下:

import glob
path =r'C:\YourFolder' #path to folder with .csv files
all = glob.glob(path+'/*SI--Exp1*.csv')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM