user2536218
user2536218

Reputation:

create a list of files from a directory with various parameters

My folder has .jpg files as follows:

D:\\myfolder\\a.b2001001.c05.jpg
D:\\myfolder\\a.b2001002.c08.jpg
D:\\myfolder\\a.b2001003.c07.jpg
D:\\myfolder\\a.b2001004.c09.jpg
...
...
D:\\myfolder\\a.b2001080.c11.jpg

How can I select only files ranging from a.b2001003 to a.b2001050? (question 1) How can I select only files containing c05, c08, and c09 ? (question 2)

import glob
files = glob.glob ("D:\\myfolder\\????.jpg")

Upvotes: 0

Views: 78

Answers (2)

Manuj Rastogi
Manuj Rastogi

Reputation: 347

import glob

import re

s = ''.join(glob.glob('/path/to/direc/*'))

files ranging from c05, c08, c09 :-

re.findall("a\.b[0-9]{7}\.c[0][5|8|9]\.JPG", s)

files ranging from a.b2001003 to a.b2001050

re.findall("a.b20010[0-5][3-9]\.JPG", s)

Hope this helps somehow ... :)

Upvotes: 0

falsetru
falsetru

Reputation: 368954

Using only glob.glob() will not produce what you want.

Using list comprehension:

import glob
import os

files = glob.glob("D:\\myfolder\\a.b*.c0[589].jpg")
files = [f for f in files if 2001003 <= int(os.path.basename(f)[3:10]) <= 2001050]

Upvotes: 3

Related Questions