简体   繁体   English

python glob.glob()regex不同目录中的多个文件

[英]python glob.glob() regex multi files in different dir

I am trying to use glob.glob() to get a list of files that comes from different directories with two kinds of suffix. 我正在尝试使用glob.glob()来获取来自具有两种后缀的不同目录的文件列表。

For example, the files i am going to read are 例如,我要读取的文件是

/ABC/DEF/HIJ/*.{data,index} /ABC/DEF/HIJ/*.{data,index}

and

/ABC/LMN/HIJ[0-3]/*.{data,index} /ABC/LMN/HIJ[0-3]/*.{data,index}

I was asked to do it with only a single glob.glob() call. 我被要求只用一个 glob.glob()调用来完成。 How can I do it? 我该怎么做? Thanks. 谢谢。

You could try using a list comprehension (if this fits your single call criteria), 您可以尝试使用列表理解功能(如果这符合您的单个通话条件),

files_wanted = ['/ABC/DEF/HIJ/*.data', '/ABC/DEF/HIJ/*.index', '/ABC/LMN/HIJ[0-3]/*.data', '/ABC/LMN/HIJ[0-3]/*.index'] #List containing your regular expressions.

files_list = [glob.glob(re) for re in files_wanted] #List comprehension.

Hope this works for you! 希望这对您有用!

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

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