cirlabs / django-project-template

A collection of development tasks and optimizations aimed at anyone doing news application development on tight deadlines in Django.
17 stars 10 forks source link

Add `all_files` to lib #91

Closed aboutaaron closed 9 years ago

aboutaaron commented 9 years ago

This will probably be a useful later

import os
import fnmatch

def all_files(root, patterns='*', single_level=False, yield_folders=False):
    """
    Expand patterns form semicolon-separated string to list
    example usage: thefiles = list(all_files('/tmp', '*.py;*.htm;*.html'))
    """
    patterns = patterns.split(';')

    for path, subdirs, files in os.walk(root):
        if yield_folders:
            files.extend(subdirs)

        files.sort()

        for name in files:
            for pattern in patterns:
                if fnmatch.fnmatch(name, pattern):
                    yield os.path.join(path, name)
                    break

        if single_level:
            break