Listing files in a bunch of folders – Python

We have a whole bunch of files and we want to crawl through them and list them.

Screen Shot 2013-02-16 at 20.56.30

Crawling through folders

This can be done using the walk() function in the os python module.

You can read all about it here, but you basically end up with a tuple, containing the directory path, the directory names, and the file names.

We are interested in the directory paths and the file names.

So to crawl through them you can run this script.

import os

for dirPath,dirNames,filName in os.walk('.'):
    for theFile in filName:
        if theFile.endswith('.py'):
            print os.path.join(dirPath,theFile)

Which returns


Leave a Reply

Please log in using one of these methods to post your comment: 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 )

Connecting to %s