[GIS] Iterate Through Folders within Folders

arcgis-10.3arcgis-desktoparcpyiteratormodelbuilder

I am having some trouble creating a model that will iterate through folders and subfolders. I want to be able to have the top level folder as a parameter and then have the code go through each of the sub folders and complete an action.

I have working code for the action I'm looking to complete and I am able to create an iteration to go through one folder, but I want to be able to go through 18 folders and run my script on every .mxd within each folder.

Here is the model that runs within one folder.
enter image description here

I want this to run through the 18 folders that I have.

Any ideas? I can run this either as a model or a script.

Best Answer

Here's how to find all the mxds in a directory tree using the os module:

import os

for root, dirs, files in os.walk('C:/junk'):
    for file in files:
        if os.path.splitext(file)[1] == '.mxd':
            print 'It is an MXD!'
Related Question