I'm trying to add a part of my filename into a field that i'd created, but it comes with the following error:

Traceback (most recent call last): File "Z:\04 - Temp\Charles\NDVI_tempo\NDVI_py3.tbx#Script_NDVIpy3.py", line 50, in File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\management.py", line 3019, in CalculateField raise e File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\management.py", line 3016, in CalculateField retval = convertArcObjectToPythonObject(gp.CalculateField_management(*gp_fixargs((in_table, field, expression, expression_type, code_block), True))) File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\geoprocessing_base.py", line 506, in return lambda *args: val(*gp_fixargs(args, True)) arcgisscripting.ExecuteError: ERROR 000539: Error running expression: test Traceback (most recent call last): File "", line 1, in NameError: name 'test' is not defined

Failed to execute (CalculateField).
Failed to execute (Script).

This is the script that i wrote:

intershp = 'Z:\\04 - Temp\\Charles\\NDVI_tempo\\NAL\\intershp.shp'

arcpy.AddField_management (intershp, "Data", "TEXT", "", "", "", "", "", "", "")

tiff_name = tiff.split("_")[0]
field_name = "{}".format(tiff_name[0:4])

arcpy.CalculateField_management (intershp, "Data", field_name, "", "")
  • File "", line 1, in NameError: name 'test' is not defined. Where is test coming from i do not see that in the code you posted. – NULL.Dude Oct 2 '17 at 14:01
  • "test" is a part of the filename, I used "for tiff in rasters" before – Charlera Oct 2 '17 at 14:04
  • 1
    intershp = r'Z:\04- you need to raw string your parameter – Midavalo Oct 2 '17 at 14:11
  • It still don't works, the shape haven't any problem at all, the information is from the raster file – Charlera Oct 2 '17 at 14:16

Actually I found the solution!

The expression field couldn't be 'field_name', it has to be "'" + field_name + "'"

  • huzzah! I was seconds away posting that answer. Good work :) Here's the documentation where I found it mentioned: pro.arcgis.com/en/pro-app/tool-reference/data-management/… – mr.adam Oct 2 '17 at 14:36
  • 1
    it's good practice to mark your own answer with the green checkmark... better to officially answer your own question than to leave an unanswered question out there. – mr.adam Oct 2 '17 at 16:42
  • "You can accept your own answer in 2 days", i'll mark it! Thanks for the advice – Charlera Oct 2 '17 at 17:40

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.