[GIS] Why does Add Field to Layer File give “ERROR 000840: The value is not a Feature Layer”

arcgis-10.0arcpyerror-000840network-analystshapefile

I'm trying to add a field to a layer file, but it always gives an error. What I did was making a closest facility layer and save the result as a layer and after that I tried to add a field, but then it gives an error. It says it cannot add a field if it isn't a raster layer, …

I tried to avoid this by making a shapefile (manually) and this can work, but is there a method to do this in Python? So convert a route from the closest facility layer to a shapefile using Python? It always says:

ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000840: The value is not a Feature Layer.

Best Answer

You might need to add the ValidateFieldName function to your script, which will replace invalid characters with an underscore, and ensure the suggested new field name does not already exist. The code below should get you started:

import arcpy  
fc = "C:/Data/NameOfData.shp" 
fieldname = arcpy.ValidateFieldName("NewFieldName") 
arcpy.AddField_management(fc, fieldname, "TEXT", "","", 12)
Related Question