Skip to content

Instantly share code, notes, and snippets.

@whophil
Last active August 2, 2018 02:55
Show Gist options
  • Save whophil/2a999bcaf0ebfbd6e5c0d213fb38f489 to your computer and use it in GitHub Desktop.
Save whophil/2a999bcaf0ebfbd6e5c0d213fb38f489 to your computer and use it in GitHub Desktop.
Recursive glob in Python: Find all files matching a glob-style pattern. Adapted from http://stackoverflow.com/a/2186565/6191541
import os
import fnmatch
def recursive_glob(rootdir='.', pattern='*'):
"""Search recursively for files matching a specified pattern.
Adapted from http://stackoverflow.com/questions/2186525/use-a-glob-to-find-files-recursively-in-python
"""
matches = []
for root, dirnames, filenames in os.walk(rootdir):
for filename in fnmatch.filter(filenames, pattern):
matches.append(os.path.join(root, filename))
return matches
@Arrrlex
Copy link

Arrrlex commented May 25, 2018

You could also write a recursive version of iglob like this:

def recursive_iglob(rootdir='.', pattern='*'):
    for root, dirnames, filenames in os.walk(rootdir):
        for filename in fnmatch.filter(filenames, pattern):
            yield os.path.join(root, filename)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment