AnsweredAssumed Answered

Layer exsitis help

Question asked by Tonyalmeida on Oct 28, 2013
Latest reply on Oct 29, 2013 by Tonyalmeida
I have a python script that i would like to turn on some layers "if they exsist", if they don't exsist i would like for the script to continue to run. The current script won't run it give me an error

  File "<string>", line 8, in <module>
IndexError: list index out of range

This script will be used on about 5 mxd for a single project.
Note: some mxd don't have the layer "URNBAN_12" OR "RURAL_12"
The mxd with these layers added to the TOB run just fine with this script, but some mxd don't have these layers on the mxd and therefore it stops at line 8..

So how can i make the script continue to run past line 8?

import arcpy import os  arcpy.env.overwriteOutput = True  mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] lyr = arcpy.mapping.ListLayers(mxd, "URBAN_12")[0]  UR = "URBAN_12" if arcpy.Exists(UR):     lyr.visible = True  lyr = arcpy.mapping.ListLayers(mxd, "Rural_12")[0]  RU = "Rural_12" if arcpy.Exists(RU):     lyr.visible = True      mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] lyr = arcpy.mapping.ListLayers(mxd, "SUBJECT_PROPERTY")[0]  arcpy.env.workspace = os.path.dirname(mxd.filePath) wp = os.path.dirname(mxd.filePath)  del mxd  mxd = arcpy.mapping.MapDocument("CURRENT") lyr = arcpy.mapping.ListLayers(mxd, "SUBJECT_PROPERTY")[0] lyrpath = lyr.workspacePath  arcpy.env.qualifiedFieldNames = False  SP = "SUBJECT_PROPERTY"   lyr.replaceDataSource(wp, "SHAPEFILE_WORKSPACE", SP, True )  df.extent = lyr.getSelectedExtent() df.scale = 2000 arcpy.RefreshActiveView() arcpy.RefreshTOC()  del df  mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] lyr = arcpy.mapping.ListLayers(mxd, "1_2")[0]  S12 = "1_2"  lyr.replaceDataSource(wp, "SHAPEFILE_WORKSPACE", S12, True )  if arcpy.Exists(S12):     df.extent = lyr.getSelectedExtent()  df.extent = lyr.getSelectedExtent()     arcpy.RefreshActiveView() arcpy.RefreshTOC() 


I appreciate any help.
Thanks.

Outcomes