Get a filtered list of files in a directory
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Riding Sky Waves v001
--
Chapters
00:00 Question
00:51 Accepted answer (Score 562)
01:05 Answer 2 (Score 170)
01:32 Answer 3 (Score 81)
02:48 Answer 4 (Score 57)
03:03 Thank you
--
Full question
https://stackoverflow.com/questions/2225...
Accepted answer links:
[glob]: http://docs.python.org/library/glob.html...
Answer 4 links:
https://docs.python.org/3/library/fnmatc...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #filesystems #wildcard #glob #directorylisting
#avk47
ACCEPTED ANSWER
Score 617
import glob
jpgFilenamesList = glob.glob('145592*.jpg')
See glob in python documenttion
ANSWER 2
Score 185
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.
ANSWER 3
Score 89
Keep it simple:
import os
relevant_path = "[path to folder]"
included_extensions = ['jpg','jpeg', 'bmp', 'png', 'gif']
file_names = [fn for fn in os.listdir(relevant_path)
if any(fn.endswith(ext) for ext in included_extensions)]
I prefer this form of list comprehensions because it reads well in English.
I read the fourth line as: For each fn in os.listdir for my path, give me only the ones that match any one of my included extensions.
It may be hard for novice python programmers to really get used to using list comprehensions for filtering, and it can have some memory overhead for very large data sets, but for listing a directory and other simple string filtering tasks, list comprehensions lead to more clean documentable code.
The only thing about this design is that it doesn't protect you against making the mistake of passing a string instead of a list. For example if you accidentally convert a string to a list and end up checking against all the characters of a string, you could end up getting a slew of false positives.
But it's better to have a problem that's easy to fix than a solution that's hard to understand.
ANSWER 4
Score 62
Another option:
>>> import os, fnmatch
>>> fnmatch.filter(os.listdir('.'), '*.py')
['manage.py']