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