简体   繁体   中英

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.

For example, the files i am going to read are

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

and

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

I was asked to do it with only a single glob.glob() call. 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!

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