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

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 comment