Print

Print


Michael,

This might help:

import os
path = [the path to the directory that contains all the sub-directories you want to work in]
for root, dirs, files in os.walk(path):
     for file in files:
          if file.split(".")[-1]==".mxd":
                [do something to it, or put it in a list for later use, etc...]
                [if you did 'print str(root)+os.sep+file' it will give you all the paths to the mxds and you could use that to do your arcpy.mapping stuff]
          else:
                pass

There could be another/better way to do it, but that should at least put you on the right path (pun intended) to find all the mxds in the sub-directories. 



Shawn
Herrick – Sr. GIS Analyst

University of New Hampshire | Facilities-Campus
Planning

22 Colovos Rd | Durham, NH 03824

(603)862-3931 | www.unh.edu/facilities



Date: Thu, 29 May 2014 12:30:25 +0000
From: [log in to unmask]
Subject: Re: Running Python Scripts on Files and Files in Subdirecto​ries
To: [log in to unmask]






If you’re confident that all of the MXDs you want to address are in the form subdirectory/document.mxd, then the glob module should work to give you a list based on the string



path + "/*/*.mxd"



You could append this to



path + "/*.mxd"



to address all of the files of interest in one list.



-- Andy



On May 29, 2014, at 8:12 AM, Michael Lachance wrote:



Hi All,

I have been working with Python quite a bit more since the Spring NeArc meeting but I'm still pretty new to it and had a question. I'm wondering how I can get a script to run not only on files in a folder, but also files in subdirectories
 within that folder. The script I put together goes into each MXD in the folder I specifiy and re-sources one of our files that has been renamed. It works, but I have MXDs saved in subdirectories and it would be so much easier to have the script run on the
 main folder "MXDs" rather than have to specify a new subdirectory everytime I want to run it on a set of MXDs.

Is this possible? My research suggests a walk function [os.walk()] or using the glob module?

Here is the code I have been using. Feel free to give any feedback on it as I am still a rookie with Python:

import arcpy, os

path = r"I:/ARC/DATA/MXDs/Weekly Report"

for fileName in os.listdir(path):

    fullPath = os.path.join(path, fileName)

    if os.path.isfile(fullPath):

        basename, extension = os.path.splitext(fullPath)

        if extension == ".mxd":

            mxd = arcpy.mapping.MapDocument(fullPath)

            print "MXD: " + fullPath

            for df in arcpy.mapping.ListDataFrames(mxd):

                for lyr in arcpy.mapping.ListLayers(mxd,"",df):

                    findthis = 'Positive Trees'

                    findthat = 'Infested Trees'

                    if findthis in lyr.name or findthat in
lyr.name: 

                        lyr.replaceDataSource(r'I:/ARC/Data/InfestedTrees/Infested_trees.gdb','FILEGDB_WORKSPACE','Trees')

                        print(lyr.name + " - Layer Resourced to Infested Trees")

                    else:

                        pass

                arcpy.RefreshTOC()

                arcpy.RefreshActiveView()

                mxd.save()

print "--Script complete--"



Thanks!


-- 

Michael Lachance