Quick & Dirty python recursive folder search

Someone asked how to have python recursively search a folder structure. There may be a better way but this is how I typically do it–it basically starts with one directory and loops through the contents compiling a list of sub-directories as it goes through the contents.

Image

import glob, os

theDir = 'c:/temp/'
theDirList = []
theDirList.append(theDir)

while len(theDirList) > 0:
    newDirList = []
    for iDir in theDirList:
        print iDir
        for iFile in glob.glob(iDir+"/*"):
            if (os.path.isdir(iFile)):
                newDirList.append(iFile)

    theDirList = newDirList
Advertisements

4 thoughts on “Quick & Dirty python recursive folder search

    • Thanks for the handy utility, jpardy84. The user I was helping needed to look at all files, just not feature classes but I know at some point that will come up so your code will be helpful.

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