xyzio

Recursively get all file names from a directory

leave a comment »

Recursively get all file names matching a substring from a directory.

Where:
* path = Directory to search
* matchStr = Substring to match

import os
import fnmatch

def getFiles(path, matchStr):
matches = []
for root, dirnames, filenames in os.walk(path):
    for filename in fnmatch.filter(filenames, matchStr):
        matches.append(os.path.join(root, filename))

return matches

Written by M Kapoor

April 12, 2015 at 2:21 pm

Posted in python

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: