[GIS] Error msg “Name is not defined” when trying to populate a field with the shape file name

arcgis-desktoperror-000539field-calculatornameerrorpython-parser

I am trying to populate a field with corresponding file name. For example, if the file name is Route1.shp then the field value will be Route1 as well. However my code is not working. Can you please suggest a code that works? The code I used is given below, along with the error message

    import arcpy, os, sys
    from arcpy import env
    arcpy.env.overwriteOutput = True
    env.workspace = r"C:\ABC\GIS"
    fc_tables = arcpy.ListFeatureClasses ("R*")
    for fc in fc_tables:
        field = "RouteName"
        arcpy.AddField_management (fc, field, "TEXT")
    for fc in fc_tables:
    arcpy.CalculateField_management (fc, field, str(fc), "PYTHON")

And this is the error message

Runtime error Traceback (most recent call last): File "",
line 2, in File "c:\program
files\arcgis\desktop10.1\arcpy\arcpy\management.py", line 3128, in
CalculateField raise e ExecuteError: ERROR 000539: Error running
expression: Route1.shp Traceback (most recent call last): File
"", line 1, in NameError: name 'Route1' is not
defined Failed to execute (CalculateField).

Best Answer

I see a couple potential errors/improvements. One is that your very last line should be indented one block so that it occurs in the for loop, but maybe that's just a formatting issue on this post, and your actual code is fine.

Issue 2 is what the error message is referring to. When calculating text fields, the value needs to be encapsulated in double quotes, like so -

arcpy.CalculateField_management (fc, field, '"' + fc + '"')

so you use single quotes as python string delimiters to encapsulate a single double quote on either side of the text value, and then concatenate it all together.

Another issue is that if you're adding a text field, you need to specify a field_length parameter, or at least I think you do. Maybe it defaults to something if you don't specify it, but I always add it. So your add_field line would be

arcpy.AddField_management (fc, field, "TEXT", field_length=50)

change the length as you see fit, depending on the length of text you intend to put in there.

Fourth- your 'field' variable is defined in a for block. That's not necessary, as you're always assigning it the value "RouteName". Plus, you then use it outside of that for block, and inside another for block. Change it like this -

field = "RouteName"    
for fc in fc_tables:            
    arcpy.AddField_management (fc, field, "TEXT", field_length=50)

fifth - no need to loop through the list twice. You can do both operations on the feature class one after another, so your code would look like this -

field = "RouteName"    
for fc in fc_tables:
    arcpy.AddField_management (fc, field, "TEXT", field_length=50)
    # No need to specify "PYTHON" here since you're not using it, and no need to cast fc to a string since it already is
    arcpy.CalculateField_management (fc, field, '"' + fc + '"')
Related Question