Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Python os.walk to identify a list of files [duplicate]

Tags:

python

Possible Duplicate:
In Python, fastest way to build a list of files in a directory with a certain extension

I currently am using os.walk to recursively scan through a directory identifying .MOV files

def fileList():
    matches = []
    for root, dirnames, filenames in os.walk(source):
        for filename in fnmatch.filter(filenames, '*.mov'):
            matches.append(os.path.join(root, filename))
    return matches

How can I extend this to identify multiple files, e.g., .mov, .MOV, .avi, .mpg?

Thanks.

like image 863
ensnare Avatar asked Dec 24 '11 17:12

ensnare


1 Answers

Try something like this:

def fileList(source):
    matches = []
    for root, dirnames, filenames in os.walk(source):
        for filename in filenames:
            if filename.endswith(('.mov', '.MOV', '.avi', '.mpg')):
                matches.append(os.path.join(root, filename))
    return matches
like image 199
Raymond Hettinger Avatar answered Sep 30 '22 13:09

Raymond Hettinger