1

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()

1 Answer 1

3

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.

6
  • +1. I regularly use your second option as I'm often comparing two values that may have come from two different places (rather than hard-coded into my code). lyr.name.lower() == otherlyr.name.lower()
    – Midavalo
    Apr 26, 2017 at 16:37
  • when I use this method if lyr.name.lower() == "Address Point".lower(): the script completes but does remove the layer form the TOC. If I then print a list of the layers the Layer "Address Point" is not in the list, like it has actually been removed, but it still remains in the TOC and draws in the MXD.
    – Ajc5286
    Apr 26, 2017 at 17:14
  • Some dumb questions then: you are working on a 'closed' MXD, so not 'current'? If so, did you save() the mxd when done? Apr 26, 2017 at 17:27
  • I running this script in the 'current' MXD. Do I need end by saving the MXD for the TOC and map to update?
    – Ajc5286
    Apr 26, 2017 at 17:54
  • If 'current', you should not need to save it. Try arcpy.RefreshTOC() Apr 26, 2017 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.