Get a filtered list of files in a directory
import glob
jpgFilenamesList = glob.glob('145592*.jpg')
See glob
in python documenttion
glob.glob()
is definitely the way to do it (as per Ignacio). However, if you do need more complicated matching, you can do it with a list comprehension and re.match()
, something like so:
files = [f for f in os.listdir('.') if re.match(r'[0-9]+.*\.jpg', f)]
More flexible, but as you note, less efficient.