Wednesday, 26 August 2015

arcgis desktop - Error msg "Name is not defined" when trying to populate a field with the shape file name


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





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