bebraw
4/29/2010 - 4:35 PM

glob_example.py

import fnmatch
import os

def locate(pattern, root=os.curdir):
    '''Locate all files matching supplied filename pattern in and below
    supplied root directory.

    Original version: http://code.activestate.com/recipes/499305/
    '''
    for path, dirs, files in os.walk(os.path.abspath(root)):
        for filename in fnmatch.filter(files, pattern):
            yield os.path.join(path, filename)

# let's find all js files in the src sibling directory (recurses!)
for file_path in locate('*.js', '../src'):
    print file_path # this should print whole path to the file
import glob

# let's find all js files in the src sibling directory (not recursive!)
for file_path in glob.iglob('../src/*.js'):
     print file_path