[GIS] arcpy.mapping.RemoveLayer function not removing layer from mxd

arcpylayers

I have a Python script that ends with a function to remove a feature layer, "Address Point", that is created at the start of the script. When I run the script it completes without any errors but does not remove the layer that is defined in the RemoveLayer function. If I list out the layers in the mxd after the script completes the layer I am trying to remove is listed with the layer name I am calling. Any ideas on why this function is falling to remove this layer?

    import arcpy
    import os

    arcpy.env.overwriteOutput = True

    # Make Layer
    arcpy.MakeFeatureLayer_management(r'Database
    Connections\10.103.2.145_BCDES_sa_ArcGIS_10.2.2.sde\BCDES.DBO.AddressPoint','Address Point')

    # Delete Feature class from file Geodatabase
    arcpy.Delete_management('C:\GIS\BlairMailer\Mailer.mdb\Address_Point')
    print 'Feature Class Deleted'

    # Select by attribute
    arcpy.SelectLayerByAttribute_management("Address Point", "NEW_SELECTION", "[PRINT_] = 'Y'" or "[PRINT_] = 'y'")
    print 'Features Selected'

    # Export selected feature to Geodatabase 
    arcpy.FeatureClassToGeodatabase_conversion("Address Point",'C:\GIS\BlairMailer\Mailer.mdb')
    print 'Feature Class Exported'

    # Open Mailer
    os.startfile(r'C:\GIS\BlairMailer\Mailer.docx')

    # Calculates Null valve for selected features
    arcpy.CalculateField_management("Address 
    Point","PRINT_","None","PYTHON_9.3")

    #Remove feature layer from map
    mxd = arcpy.mapping.MapDocument(r'C:\Users\achapman\Desktop\Addressing.mxd')
    for df in arcpy.mapping.ListDataFrames(mxd):
    for lyr in arcpy.mapping.ListLayers(mxd, "", df):
        if lyr.name.lower() == "Address Point":
            arcpy.mapping.RemoveLayer(df, lyr)

Revised my script and this is working now.

import arcpy
import os

arcpy.env.overwriteOutput = True
currentMXD = arcpy.mapping.MapDocument("current")
activeDataFrame = currentMXD.activeDataFrame

# Make Layer
arcpy.MakeFeatureLayer_management(r'Database Connections\10.103.2.145_BCDES_sa_ArcGIS_10.2.2.sde\BCDES.DBO.AddressPoint','Address Point')

# Delete Feature class from file Geodatabase
arcpy.Delete_management('C:\GIS\BlairMailer\Mailer.mdb\Address_Point')
print 'Feature Class Deleted'

# Select by attribute
arcpy.SelectLayerByAttribute_management("Address Point", "NEW_SELECTION", "[PRINT_] = 'Y'" or "[PRINT_] = 'y'")
print 'Features Selected'

# Export selected feature to Geodatabase
arcpy.FeatureClassToGeodatabase_conversion("Address Point",'C:\GIS\BlairMailer\Mailer.mdb')
print 'Feature Class Exported'

# Calculates Null valve for selected features
arcpy.CalculateField_management("Address Point","PRINT_","None","PYTHON_9.3")

# Open Mailer
os.startfile(r'C:\GIS\BlairMailer\Mailer.docx')

# Remove Address Point layer
layerList = arcpy.mapping.ListLayers(currentMXD, "", activeDataFrame)
for layer in layerList:
    if layer.name == "Address Point":
         arcpy.mapping.RemoveLayer(activeDataFrame, layer)

arcpy.RefreshTOC()

Best Answer

This line will never be true:

if lyr.name.lower() == "Address Point":

Try:

if lyr.name.lower() == "address point":

The lower() method makes the string all in lowercase; the string to compare must be an exact match, so also in all lowercase.

Yet another option would be:

if lyr.name.lower() == "Address Point".lower():

Here you lower() both ends of the condition.

For completeness, this StackOverflow answer has plenty more detail on how to handle unicode, casefolding, etc.