2

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).

1

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 + '"')
  • On the point about field length, if not expressed it will default to a length 255. – DWynne May 6 '15 at 21:38
  • Thanks for your reply! sorry I actually had the last line indented but it did not reflect on the code I posted here. – Python-Novice May 6 '15 at 21:46
1

On the last line, CalculateField really needs an extra level of quotes on the expression to make that work. Because otherwise the expression is being evaluated by the tool in Python and seeing it as a variable, hence the NameError: name 'Route1' is not defined that is being bubbled up by the tool. So this should solve your problem:

arcpy.CalculateField_management (fc, field, repr(fc), "PYTHON")

Or even:

arcpy.CalculateField_management(fc, field, '"{}"'.format(fc), "PYTHON")
  • 1
    Yes! repr(fc) worked perfect, did not try the second one yet. But thanks so much for your help! – Python-Novice May 6 '15 at 21:43

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.