1

I'm have difficulty with my script Arcmap v10.5.1. I get a syntax error on line 8. My question is: Can I calculate the Added field from an existing field within an iterated value? If so, what's my syntax error preventing me from doing so. If not, how should I proceed? my workspace is a feature dataset

import arcpy, os, sys

arcpy.env.workspace = arcpy.GetParameterAsText(0)

for fc in arcpy.ListFeatureClasses():
    arcpy.AddMessage("Adding fields and updating " + fc + "...")
    arcpy.AddField_management (fc, "KM_FontSize", "DOUBLE")
    arcpy.CalculateField_management(fc, "KM_FontSize", "!FontSize!")
    arcpy.AddField_management (fc, "KM_VerAlign", "SHORT")
    arcpy.CalculateField_management(fc, "KM_VerAlign", "!VerAlign!")
    arcpy.AddField_management (fc, "KM_HorAlign", "SHORT")
    arcpy.CalculateField_management(fc, "M_HorAlign", "!HorAlign!")
    arcpy.AddField_management (fc, "KM_XOffset", "DOUBLE")
    arcpy.CalculateField_management(fc, "KM_XOffset", "!XOffset!")
    arcpy.AddFieldmanagement (fc, "KM_YOffset", "DOUBLE")
    arcpy.CalculateField_management(fc, "KM_YOffset", "!YOffset!")
    arcpy.AddMessage("Finished updating " + fc + "...")

And here's the error message:

Traceback (most recent call last):
  File "H:\Python Scripts\KM_Utility\Create Anno Fields.py", line 10, in <module>
    arcpy.CalculateField_management(fc, "KM_FontSize", "!FontSize!")
  File "c:\program files (x86)\arcgis\desktop10.5\arcpy\arcpy\management.py", line 3661, in CalculateField
    raise e
ExecuteError: ERROR 999999: Error executing function.
Syntax error
Failed to execute (CalculateField).
  • 2
    Default language is VB and you are providing the field name enclosed with !! which is python syntax. Try: arcpy.CalculateField_management(fc, "KM_FontSize", "!FontSize!", "PYTHON") – BERA Mar 20 at 17:08
  • 1
    Thank you. I was reading the syntax for Pro. FYI> in Pro the default is Python3 – Chris Swanson Mar 20 at 17:54
2

As BERA mentioned in comments, you need to specify PYTHON as your expression type. The default is VB.

arcpy.CalculateField_management(fc, "KM_FontSize", "!FontSize!", "PYTHON")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.