Most convenient way of regex searching recursively for files in python


def find_files(directory, pattern):
for root, dirs, files in os.walk(directory):
for basename in files:
if fnmatch.fnmatch(basename.lower(), pattern):
filename = os.path.join(root, basename)
yield filename

Its copied from stackoverflow, but it shows a really good example on when to use yield. Basically it returns a generator and thats far more memory efficient than returning a list.

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s