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
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