After looking for sample code in the arcpy site help and skimming 55 pages of past python forum threads I ended up with some sample code, that I modified, that should save multiple gis layers to mulitple mxds. The script runs when I specify folder paths on my C drive. Hopefully, this forum post saves someone the trouble of skimming past forum pages, looking for sample code. However, the script does not run when I change the folder pathways. I have arcgis 10.0
Specifically, I store the two gis layers in a folder, TestPython2 and I store several mxds in a folder TestPython1, both on my S drive. After changing just the line 4 and line 5 folder pathways to point at these folders on my server, "S", drive & running the script, I get an "Invalid MXD filename" error message. I get the error when I attempt to run the script from the same or a diff folder than the one the mxds are stored in. I've tried running the the script, after storing the script, the mxds, and the layers all in the same folder. I've tried running the script from the C drive.
Does anyone see a syntax error in the code? Would it help if I change my pathways to some other configuration? I'd appreciate any other suggestions on how I could run the script.
Ken
import arcpy
import os
import glob
## Add your MXDs and Layers below
layers_path = 'S:\\Users Shared\\LOpperman\\Town Scale MXDs\\TestPython2\\'
mxds_path = 'S:\\Users Shared\\LOpperman\\Town Scale MXDs\\MXDs\\TestPython1\\'
layers = []
mxds = []
for infile in glob.glob( os.path.join( layers_path, '*.lyr') ):
layers.append(infile)
for infile in glob.glob( os.path.join( mxds_path, '*.mxd') ):
mxds.append(infile)
for mxd_name in mxds:
mxd = arcpy.mapping.MapDocument(mxds_path + mxd_name)
print 'Adding layers for: ' + mxd_name
for layer_name in layers:
print 'Adding layer: ' + layer_name
addLayer = arcpy.mapping.Layer(layers_path + layer_name)
df = arcpy.mapping.ListDataFrames(mxd, "Primary")[0]
arcpy.mapping.AddLayer(df, addLayer, "TOP")
mxd.save()
Specifically, I store the two gis layers in a folder, TestPython2 and I store several mxds in a folder TestPython1, both on my S drive. After changing just the line 4 and line 5 folder pathways to point at these folders on my server, "S", drive & running the script, I get an "Invalid MXD filename" error message. I get the error when I attempt to run the script from the same or a diff folder than the one the mxds are stored in. I've tried running the the script, after storing the script, the mxds, and the layers all in the same folder. I've tried running the script from the C drive.
Does anyone see a syntax error in the code? Would it help if I change my pathways to some other configuration? I'd appreciate any other suggestions on how I could run the script.
Ken
import arcpy
import os
import glob
## Add your MXDs and Layers below
layers_path = 'S:\\Users Shared\\LOpperman\\Town Scale MXDs\\TestPython2\\'
mxds_path = 'S:\\Users Shared\\LOpperman\\Town Scale MXDs\\MXDs\\TestPython1\\'
layers = []
mxds = []
for infile in glob.glob( os.path.join( layers_path, '*.lyr') ):
layers.append(infile)
for infile in glob.glob( os.path.join( mxds_path, '*.mxd') ):
mxds.append(infile)
for mxd_name in mxds:
mxd = arcpy.mapping.MapDocument(mxds_path + mxd_name)
print 'Adding layers for: ' + mxd_name
for layer_name in layers:
print 'Adding layer: ' + layer_name
addLayer = arcpy.mapping.Layer(layers_path + layer_name)
df = arcpy.mapping.ListDataFrames(mxd, "Primary")[0]
arcpy.mapping.AddLayer(df, addLayer, "TOP")
mxd.save()