I am trying to get a list of files in a directory using Python, but I do not want a list of ALL the files.
What I essentially want is the ability to do something like the following but using Python and not executing ls.
ls 145592*.jpg
If there is no built-in method for this, I am currently thinking of writing a for loop to iterate through the results of an os.listdir()
and to append all the matching files to a new list.
However, there are a lot of files in that directory and therefore I am hoping there is a more efficient method (or a built-in method).
You can use pathlib that is available in Python standard library 3.4 and above.
you might also like a more high-level approach (I have implemented and packaged as findtools):
can be installed with
Filter with
glob
module:Import glob
Wild Cards:
Fiter extension
.txt
:A single character
Number Ranges
Alphabet Ranges
Keep it simple:
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.
You can define pattern and check for it. Here I have taken both start and end pattern and looking for them in the filename. FILES contains the list of all the files in a directory.
glob.glob('145592*.jpg')