<?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: Removing a layer from multiple mxds in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/removing-a-layer-from-multiple-mxds/m-p/44340#M3506</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Wayne,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; Incredible. I followed your advice to make the case all the same and determined that a one letter change in the code, replacing the cap D, in the Dams variable of line 5 of my original script, solved the problem. Not only was the Dams layer removed, but it removed the layer, as spelled "Dams" in the mxds. In other words I didn't have to change to all one case for the 100 mxds that have the Dams layer. It was not necessary to run my original script in the same folder as the multiple mxds. So, that original script will remove any layer, specified in the line 5, as long as the name in the code is all lower case. Incredible how quick you spotted that problem in our code!&amp;nbsp; Thanks so much.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Ken&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 16 Mar 2013 01:01:15 GMT</pubDate>
    <dc:creator>KenLucas</dc:creator>
    <dc:date>2013-03-16T01:01:15Z</dc:date>
    <item>
      <title>Removing a layer from multiple mxds</title>
      <link>https://community.esri.com/t5/python-questions/removing-a-layer-from-multiple-mxds/m-p/44337#M3503</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I attempted to modify a sample code to remove just one layer from one of the two data frames, "Primary", I have in multiple mxds, all stored in the same folder.&amp;nbsp; This code runs, but does not actually remove the layer, "Dams", from the two text mxds, stored in the folder, RemoveLayers1.&amp;nbsp; I've specified upper, considering Dams starts with a cap D, but maybe it requires a diff command when caps are mixed with lower case.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm hoping someone can see what I'm missing. I would expect the code to provide an error message; not just fail to remove the layer.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy import os import glob mxds_path = r'C:\\Temp\\removelayers1\\' layer_name = r'Dams' data_frame = r'Primary' layers = [] mxds = []&amp;nbsp;&amp;nbsp; for infile in glob.glob(os.path.join( mxds_path, "*.mxd" )): &amp;nbsp; mxd = arcpy.mapping.MapDocument(infile) &amp;nbsp; print "removing Dams layer from "&amp;nbsp; + infile &amp;nbsp; for df in arcpy.mapping.ListDataFrames(mxd, data_frame): &amp;nbsp;&amp;nbsp;&amp;nbsp; for lyr in arcpy.mapping.ListLayers(mxd, "", df): &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #print lyr &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.name.upper() == layer_name: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'Remove layer ' + layer_name &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.mapping.RemoveLayer(df, lyr) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #for mxd_name in mxds: &amp;nbsp; #mxd = arcpy.mapping.MapDocument(mxds_path + mxd_name) &amp;nbsp; #print 'removing layer for ' + mxd_name &amp;nbsp;&amp;nbsp; #&amp;nbsp; mxd.save()&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Ken&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 14 Mar 2013 23:11:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/removing-a-layer-from-multiple-mxds/m-p/44337#M3503</guid>
      <dc:creator>KenLucas</dc:creator>
      <dc:date>2013-03-14T23:11:28Z</dc:date>
    </item>
    <item>
      <title>Re: Removing a layer from multiple mxds</title>
      <link>https://community.esri.com/t5/python-questions/removing-a-layer-from-multiple-mxds/m-p/44338#M3504</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello Ken,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I think the main problem here is the expectation of what you'd get with the functions upper/lower.&amp;nbsp; So I'll demonstrate with IDLE:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;&amp;gt;&amp;gt;&amp;gt; someText = 'IamAlayer' &amp;gt;&amp;gt;&amp;gt; print someText.upper() IAMALAYER &amp;gt;&amp;gt;&amp;gt; print someText.lower() iamalayer &amp;gt;&amp;gt;&amp;gt; &lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;As you can see, the function 'lower' returns all lower case; the function 'upper' returns all upper case.&amp;nbsp; It is that cut and dried.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;So the only way your 'if' statement to be true as you need it to be to execute the 'removeLayer' part of the code, you at least need to feed in the proper conditional variables...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So you could go with 'lower' as you had the code originally - which for the layer in question would return 'dams' (if there is such a layer in the map's data frame), so you'd need to set the 'layer_name' variable to the following (not with the 1st letter capitalized):&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;layer_name = r'dams'&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;extra clarification:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;As further explanation by example, it doesn't matter whether you use 'upper' or 'lower', you cannot use mixed case on the variable 'layer_name'....you should define it as either 'dams' (lower) or 'DAMS' (upper) as shown:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;&amp;gt;&amp;gt;&amp;gt; if layer_name.lower() == 'dams':&amp;nbsp; print 'yes, we have a match - go ahead with the rest code block.'&amp;nbsp;&amp;nbsp;&amp;nbsp; yes, we have a match - go ahead with the rest code block. &amp;gt;&amp;gt;&amp;gt; if layer_name.upper() == 'DAMS':&amp;nbsp; print 'yes, we have a match - again, proceed...'&amp;nbsp;&amp;nbsp;&amp;nbsp; yes, we have a match - again, proceed... &amp;gt;&amp;gt;&amp;gt; if layer_name.lower() == 'Dams':&amp;nbsp; print 'go go go proceed with the code block...' else:&amp;nbsp; print 'no way....we do not agree - halt this block!'&amp;nbsp;&amp;nbsp;&amp;nbsp; no way....we do not agree - halt this block! &amp;gt;&amp;gt;&amp;gt;&amp;nbsp; &amp;gt;&amp;gt;&amp;gt; if layer_name.upper() == 'Dams':&amp;nbsp; print 'go go go proceed with the code block...' else:&amp;nbsp; print 'no way....we do not agree - halt this block!'&amp;nbsp;&amp;nbsp;&amp;nbsp; no way....we do not agree - halt this block! &amp;gt;&amp;gt;&amp;gt; &lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;At any rate, on your 'if' statement, the condition lyr.name.upper() == r'DAMS' [or lyr.name.lower() == r'dams'] must be met for 'arcpy.mapping.RemoveLayer(df, lyr)' to execute...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If you have any questions about that, let me know.&amp;nbsp; Also, you will need to save the mxd (whether you use 'mxd.save()' or 'mxd.saveACopy(...)', it doesn't matter, you must save the mxd for any changes to persist.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Enjoy,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Wayne&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 14 Mar 2013 23:42:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/removing-a-layer-from-multiple-mxds/m-p/44338#M3504</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2013-03-14T23:42:03Z</dc:date>
    </item>
    <item>
      <title>Re: Removing a layer from multiple mxds</title>
      <link>https://community.esri.com/t5/python-questions/removing-a-layer-from-multiple-mxds/m-p/44339#M3505</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy, os, glob
mxds_path = r'C:\Temp\removelayers1'
layer_name = 'DAMS'

for infile in glob.glob(os.path.join( mxds_path, "*.mxd")):
&amp;nbsp; mxd = arcpy.mapping.MapDocument(infile)
&amp;nbsp; for df in arcpy.mapping.ListDataFrames(mxd, ""):
&amp;nbsp;&amp;nbsp;&amp;nbsp; for lyr in arcpy.mapping.ListLayers(mxd, "", df):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.name.upper() == layer_name:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.mapping.RemoveLayer(df, lyr)
mxd.save()
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;...simple as that, I would think works -- check it and see (I have not tested it).&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 21:42:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/removing-a-layer-from-multiple-mxds/m-p/44339#M3505</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2021-12-10T21:42:04Z</dc:date>
    </item>
    <item>
      <title>Re: Removing a layer from multiple mxds</title>
      <link>https://community.esri.com/t5/python-questions/removing-a-layer-from-multiple-mxds/m-p/44340#M3506</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Wayne,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; Incredible. I followed your advice to make the case all the same and determined that a one letter change in the code, replacing the cap D, in the Dams variable of line 5 of my original script, solved the problem. Not only was the Dams layer removed, but it removed the layer, as spelled "Dams" in the mxds. In other words I didn't have to change to all one case for the 100 mxds that have the Dams layer. It was not necessary to run my original script in the same folder as the multiple mxds. So, that original script will remove any layer, specified in the line 5, as long as the name in the code is all lower case. Incredible how quick you spotted that problem in our code!&amp;nbsp; Thanks so much.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Ken&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 16 Mar 2013 01:01:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/removing-a-layer-from-multiple-mxds/m-p/44340#M3506</guid>
      <dc:creator>KenLucas</dc:creator>
      <dc:date>2013-03-16T01:01:15Z</dc:date>
    </item>
  </channel>
</rss>

