简体   繁体   中英

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. 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 . 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-- . 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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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