I previously successfully used the sample code here: How to calculate Field using part of filename in ModelBuilder?
# Import standard library modules import arcpy, os, sys from arcpy import env # Allow for file overwrite arcpy.env.overwriteOutput = True # Set the workspace directory env.workspace = r"C:\temp.gdb" # Get the list of the featureclasses to process fc_tables = arcpy.ListFeatureClasses() # Loop through each file and perform the processing for fc in fc_tables: print str("processing " + fc) # Define field name and expression field = "SID" expression = str(fc[:5]) #subsets first 5 characters of fc name # Create a new field with a new name arcpy.AddField_management(fc,field,"TEXT") # Calculate field here arcpy.CalculateField_management(fc, field, "expression", "PYTHON")
But now I'm running it again, with no changes, even with the same datasets I used before, and it no longer works. I cannot see how or why it errors out.
Runtime error : ERROR 000539: Error running expression: expression : name 'expression' is not defined Failed to execute (CalculateField).