简体   繁体   English

Python Glob 无法识别路径字符串中包含任意数字 2 到 12 的文件模式

[英]Python Glob doesn't recognize file pattern with arbitary numbers 2 to 12 in a path string

Is there a way to get an OR condition inside the file path for glob to recognize it?有没有办法在文件路径中获取OR条件以便 glob 识别它? something like source_path = path + ('[1-9]+'|'[1-9][0-1]+') , mainly to recognize file with filename as (just an exaample): source_path = path + ('[1-9]+'|'[1-9][0-1]+')类的东西,主要是将文件名识别为(只是一个例子):

filename_2, 
filename_3, 
filename_10, 
filename_11,
import glob 
source_path = path + '[1-11]*'
for file in gb.glob(source_path): 
    print('file=', file) #'--gives no output

source_path = path + '[1-9]*'
for file in glob.glob(source_path):
    print('file=', file)

This code only detects filename_2 and filename_3 .此代码仅检测filename_2filename_3

Number of items to match the file name: 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11 something like this works:与文件名匹配的项目数: 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11这样的工作:

0[1-9]|1[0-1]

For more information read this .regular-expressions.info有关更多信息,请阅读此.regular-expressions.info

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

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