[GIS] Debugging ERROR 000725 from loop using SearchCursor, SelectLayerByAttribute, and CopyFeatures

arcpyerror-000725select-by-attributesymbology

I am fairly new to python scripting for ArcGIS. I am trying to create maps where each county is selected and then copied into a new layer with only that county's data. Then, I used ApplySymbologyFrom to copy the symbology format from a mock layer. It worked when I ran the code for an individual county, but it doesn't work when I try to use it in a loop statement.

Error details: The code seems to be working up until the loop step. The variables are stored, environment workspace is set, and the current map is being referenced. Cursor variable is stored. When I try to run the code block "for row in cursor" and press enter twice, the program does not have any response besides a new >>> line. There is no error code produced when I run that code block.

Error message from PythonWin run: ExecuteError: Failed to execute. Parameters are not valid. ERROR 000725: Output Feature Class: Dataset T:\HCI\Shapefiles\Inset_Maps\Alameda.shp already exists. Failed to execute (CopyFeatures).

UPDATE: I added the code to delete previous data. Now the new error message is "NameError: name 'row' is not defined"

Here's my code so far:

# Import system modules
import arcpy

# Set workspace
arcpy.env.workspace = r"T:\HCI\Shapefiles\Inset_Maps"
#mxd = arcpy.mapping.MapDocument("CURRENT")
mxd = arcpy.mapping.MapDocument("C:\Users\jchan1\Desktop\Inset_maps_test.mxd")

#background processing was disabled in geoprocessing options in order to access current map
#geoprocessing output overwrite enabled in geoprocessing options
# create local variables
field = "NAME_1"
counties = "Counties_layer"

#remove field delimiters, if needed
arcpy.AddFieldDelimiters(counties,field)

cursor = arcpy.SearchCursor(counties)


for row in cursor:
    whereClause = '"NAME_1" = \'' + row.getValue(field) + "'"
    if arcpy.Exists(row.getValue(field)):
        arcpy.Delete_management(row.getValue(field))
    # Select layer by county name
    arcpy.SelectLayerByAttribute_management(counties,"NEW_SELECTION",whereClause)
    # Write the selected features to a new featureclass
    arcpy.CopyFeatures_management(counties, row.getValue(field))
    # Copy county layer's symbology from sample layer
    arcpy.ApplySymbologyFromLayer_management(row.getValue(field), "dummy")

# export inset image to JPEG
# arcpy.mapping.ExporttoPJEG(mxd,r"file_path_name")

# delete local variables
del row, cursor, mxd

#Refresh screen
arcpy.RefreshTOC()
arcpy.RefreshActiveView()

Best Answer

If you are running this in the Python window, then you need go to the Geoprocessing menu -> Geoprocessing Options and check the setting for "Overwrite the outputs of geoprocessing operations". If it is not checked than the script is behaving correctly, since your current setting is not supposed to not let tools overwrite outputs. Once that setting is checked, then outputs should be overwritten if you run your tools inside ArcMap and your script should succeed.

However, for a standalone script run outside of ArcMap the geoprocessing option does not apply. You have to add code to check if a feature class already exists and delete it before trying to use Copy_Features to recreate it. That approach always works whether or not you are in ArcMap and even if the Overwrite option is not checked while you are working in ArcMap, so I always use that method. Use code like:

if arcpy.Exists(row.getValue(field)):
    arcpy.Delete_management(row.getValue(field))
# the data is deleted so now you can reuse that name for new data

If this code will be used in a script tool in ArcMap and is for end users who should get dummy warnings if they try to overwrite existing data with geoprocessing tools apart from your tool, you do not want to permanently change the overwrite setting in your script without setting it back to the previous overwrite status when the script succeeds in recreating the data. In that case have the script first read their existing overwrite setting and store it in a variable, then temporarily enable overwrite, and then reset the overwrite to the user's original setting when you are done overwriting data. Something like:

from arcpy import env
origSetting = env.overwriteOutput
env.overwriteOutput = True
# Do some data overwrites
env.overwriteOutput = origSetting