views:

381

answers:

3

Hi,

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).

Thanks.

+8  A: 

glob.glob('145592*.jpg')

Ignacio Vazquez-Abrams
Thanks. I figured it would have to be something simple like that.
mhost
Oh, I just noticed that the Python docs say glob() "is done by using the os.listdir() and fnmatch.fnmatch() functions in concert, and not by actually invoking a subshell". In other words, glob() doesn't have the efficiency improvements one might expect.
benhoyt
A: 

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.

benhoyt
A: 

use os.walk to recursively list your files

import os
root="/home"
pattern="145992"
alist_filter = ['jpg','bmp','png','gif'] 
path=os.path.join(root,"mydir_to_scan")
for r,d,f in os.walk(path):
    for file in f:
        if f[-3:] in alist_filter and pattern in file:
            print os.path.join(root,file)
ghostdog74