This question already has an answer here:
I have a seemingly simple code which iterates through a list of feature classes and calculates an ID field in the attribute table that is equal to the first 5 characters of feature class name. The script works fine if I hard code the workspace. As soon as I change it to "Get Parameter as Text" I receive this error:
ERROR 000539: Error running expression: expression : name 'expression' is not defined Failed to execute (CalculateField).
The script is:
import arcpy, sys, array, time, datetime #Get feature dataset inws = arcpy.GetParameterAsText(0) #Set geoprocessing environments arcpy.env.workspace = inws arcpy.env.overwriteOutput = True #Iterate through the list of feature classes fcList = arcpy.ListFeatureClasses() for fc in fcList: print str("processing" + fc) #Define field and expression field = "SID" expression = str(fc[:5]) #Create a new field name and expression arcpy.AddField_management(fc, field, "TEXT") arcpy.CalculateField_management(fc, field, "expression", "PYTHON")
does anyone know what is going on here? Thanks!