Directories Simplified With Generator Functions

Python’s generator and coroutine functions are among my favorite features in Python. Before I start showing how you can traverse a system’s directory, I’m going to show you a bare bone generator function.  All of these code snippets and more can be found in talk by David Beazley, Generator Tricks for Systems Programmers.  I highly recommend you watch his presentation or get his PowerPoint, you’ll uncover amazing stuff that will make your mind explode.

def generatorFunction(num):
    while num > 0:
            num -= 1

This generator expression is will count down a number all the way to zero. To use it we have to make generator expression like this.

generatorExpression = generatorFunction(5)

“Iteration is the Glue”

Generator expressions are iterables. We can use a .__next__() call to advance the generator expression and it will print out the number five. Since the generator expression support iteration, we can also throw it inside a for loop to count down zero.

for number in generatorExpression:

The yield statement in this case is like a re-callable return statement that we can have control over. Lets move on to something a bit more interesting and the topic of this post, traversing through a directory with a generator.

import os
import fnmatch

def find( pattern, tree ):
    for path, dirList, fileList in os.walk( tree ):
        for name in fnmatch.filter( fileList, pattern ):
            yield os.path.join( path, name )

All it takes is three lines of code. You can do something like this find all python scripts stored in a computer.

seeker = find( "*.py", "/" )

for fileName in seeker:

Yep, we can use a wild card character and only look for the last three characters in a file name “.py”. Running the generator expression, seeker, will printout every “.py” file in the host machine.


Leave a Reply

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

You are commenting using your 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