<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Layer exsitis help in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/layer-exsitis-help/m-p/404541#M31863</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Excellent thank you.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 28 Oct 2013 19:54:18 GMT</pubDate>
    <dc:creator>TonyAlmeida</dc:creator>
    <dc:date>2013-10-28T19:54:18Z</dc:date>
    <item>
      <title>Layer exsitis help</title>
      <link>https://community.esri.com/t5/python-questions/layer-exsitis-help/m-p/404539#M31861</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;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 &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; File "&amp;lt;string&amp;gt;", line 8, in &amp;lt;module&amp;gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;IndexError: list index out of range&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This script will be used on about 5 mxd for a single project.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Note: some mxd don't have the layer "URNBAN_12" OR "RURAL_12"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;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..&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So how can i make the script continue to run past line 8?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy import os&amp;nbsp; arcpy.env.overwriteOutput = True&amp;nbsp; mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] lyr = arcpy.mapping.ListLayers(mxd, "URBAN_12")[0]&amp;nbsp; UR = "URBAN_12" if arcpy.Exists(UR): &amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.visible = True&amp;nbsp; lyr = arcpy.mapping.ListLayers(mxd, "Rural_12")[0]&amp;nbsp; RU = "Rural_12" if arcpy.Exists(RU): &amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.visible = True &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] lyr = arcpy.mapping.ListLayers(mxd, "SUBJECT_PROPERTY")[0]&amp;nbsp; arcpy.env.workspace = os.path.dirname(mxd.filePath) wp = os.path.dirname(mxd.filePath)&amp;nbsp; del mxd&amp;nbsp; mxd = arcpy.mapping.MapDocument("CURRENT") lyr = arcpy.mapping.ListLayers(mxd, "SUBJECT_PROPERTY")[0] lyrpath = lyr.workspacePath&amp;nbsp; arcpy.env.qualifiedFieldNames = False&amp;nbsp; SP = "SUBJECT_PROPERTY"&amp;nbsp;&amp;nbsp; lyr.replaceDataSource(wp, "SHAPEFILE_WORKSPACE", SP, True )&amp;nbsp; df.extent = lyr.getSelectedExtent() df.scale = 2000 arcpy.RefreshActiveView() arcpy.RefreshTOC()&amp;nbsp; del df&amp;nbsp; mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] lyr = arcpy.mapping.ListLayers(mxd, "1_2")[0]&amp;nbsp; S12 = "1_2"&amp;nbsp; lyr.replaceDataSource(wp, "SHAPEFILE_WORKSPACE", S12, True )&amp;nbsp; if arcpy.Exists(S12): &amp;nbsp;&amp;nbsp;&amp;nbsp; df.extent = lyr.getSelectedExtent()&amp;nbsp; df.extent = lyr.getSelectedExtent()&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.RefreshActiveView() arcpy.RefreshTOC() &lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I appreciate any help.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 28 Oct 2013 17:57:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/layer-exsitis-help/m-p/404539#M31861</guid>
      <dc:creator>TonyAlmeida</dc:creator>
      <dc:date>2013-10-28T17:57:29Z</dc:date>
    </item>
    <item>
      <title>Re: Layer exsitis help</title>
      <link>https://community.esri.com/t5/python-questions/layer-exsitis-help/m-p/404540#M31862</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Tony,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You can add a try/except to your code.&amp;nbsp; Example:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy import os&amp;nbsp; arcpy.env.overwriteOutput = True&amp;nbsp; mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] try: &amp;nbsp;&amp;nbsp;&amp;nbsp; lyr = arcpy.mapping.ListLayers(mxd, "URBAN_12")[0] except IndexError: &amp;nbsp;&amp;nbsp;&amp;nbsp; pass&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 28 Oct 2013 18:23:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/layer-exsitis-help/m-p/404540#M31862</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2013-10-28T18:23:31Z</dc:date>
    </item>
    <item>
      <title>Re: Layer exsitis help</title>
      <link>https://community.esri.com/t5/python-questions/layer-exsitis-help/m-p/404541#M31863</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Excellent thank you.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 28 Oct 2013 19:54:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/layer-exsitis-help/m-p/404541#M31863</guid>
      <dc:creator>TonyAlmeida</dc:creator>
      <dc:date>2013-10-28T19:54:18Z</dc:date>
    </item>
    <item>
      <title>Re: Layer exsitis help</title>
      <link>https://community.esri.com/t5/python-questions/layer-exsitis-help/m-p/404542#M31864</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;The current script works great for mxd that have layers in the TOC; Subject_Property, URBAN_12 and RURAL_12 and 1_2. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Mxd's that do not have 1_2 in the TOC give the following error&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Runtime error &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Traceback (most recent call last):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; File "&amp;lt;string&amp;gt;", line 29, in &amp;lt;module&amp;gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;NameError: name 'lyr' is not defined&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If i don't add line 19 "del mxd, df, lyr". The Subject_property will be updated by 1_2 feature class.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
 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, "SUBJECT_PROPERTY")[0]
lyrpath = lyr.workspacePath

arcpy.env.qualifiedFieldNames = False
arcpy.env.workspace = os.path.dirname(mxd.filePath)
wp = os.path.dirname(mxd.filePath)

SP = "SUBJECT_PROPERTY" 

lyr.replaceDataSource(wp, "SHAPEFILE_WORKSPACE", SP, True )

del mxd, df, lyr

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr = arcpy.mapping.ListLayers(mxd, "1_2")[0]
except IndexError:
&amp;nbsp;&amp;nbsp;&amp;nbsp; pass
S12 = "1_2" 
lyr.replaceDataSource(wp, "SHAPEFILE_WORKSPACE", S12, True )



if arcpy.Exists(S12):
&amp;nbsp;&amp;nbsp;&amp;nbsp; df.extent = lyr.getSelectedExtent()


del mxd, df

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr = arcpy.mapping.ListLayers(mxd, "URBAN_12")[0]
except IndexError:
&amp;nbsp;&amp;nbsp;&amp;nbsp; pass

UR = "URBAN_12"
if arcpy.Exists(UR):
&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.visible = True

try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr = arcpy.mapping.ListLayers(mxd, "Rural_12")[0]
except IndexError:
&amp;nbsp;&amp;nbsp;&amp;nbsp; pass

RU = "Rural_12"
if arcpy.Exists(RU):
&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.visible = True

df.extent = lyr.getSelectedExtent()
df.scale = 2000
arcpy.RefreshActiveView()
arcpy.RefreshTOC()&amp;nbsp;&amp;nbsp; 
 
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 18:25:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/layer-exsitis-help/m-p/404542#M31864</guid>
      <dc:creator>TonyAlmeida</dc:creator>
      <dc:date>2021-12-11T18:25:45Z</dc:date>
    </item>
  </channel>
</rss>

