Tuesday 30 January 2018

field calculator - Debugging ERROR 000539 from CalculateField in ArcPy?


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.


Error message:



Runtime error : ERROR 000539: Error running expression: expression : name 'expression' is not defined Failed to execute (CalculateField).





No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...