Print

Print


Hi All,
I have been working with Python quite a bit more since the Spring
NeArcmeeting 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

------------------------------------------------------------------------- This list (NEARC-L) is an unmoderated discussion list for all NEARC Users.

If you no longer wish to receive e-mail from this list, you can remove yourself by going to http://listserv.uconn.edu/nearc-l.html.