I'm using this code as a python script tool in ArcGIS 10.1:
import arcpy, os
arcpy.env.workspace = r'W:\S&P\s&p techs\Emily\Errors.gdb'
#Looping through dissolved feature classes, adding 'Name' field and writing
#feature class name in the added field.
fcs = arcpy.ListFeatureClasses()
for fc in fcs:
arcpy.AddField_management(fc, "Name", "TEXT", field_length = 50)
with arcpy.da.UpdateCursor(fc, "Name") as cursor:
for row in cursor:
row[0] = fc
cursor.updateRow(row)
All it's supposed to do is add a field to some feature classes in a database. I double checked that the path is right, so I don't think that's the problem. This code has also worked before, but for some reason it's not working anymore. I get an error that says this when I run it:
enter image description here
1 Answer 1
Code works fine with single quote on my machine. I think it is something to do with gdb naming, (& character?). See if slightly modified code will help to track what's wrong
# Import arcpy module
import arcpy, traceback, os, sys
try:
def showPyMessage():
arcpy.AddMessage(str(time.ctime()) + " - " + message)
## arcpy.env.workspace = r'W:\S&P\s&p techs\Emily\Errors.gdb'
arcpy.env.workspace = r'C:\URS-Data\URS-Data\From_MXD\Scratch.gdb'
#Looping through dissolved feature classes, adding 'Name' field and writing
#feature class name in the added field.
fcs = arcpy.ListFeatureClasses()
for fc in fcs:
arcpy.AddMessage(fc)
arcpy.AddField_management(fc, "Name", "TEXT", field_length = 50)
with arcpy.da.UpdateCursor(fc, "Name") as cursor:
for row in cursor:
row[0] = fc
cursor.updateRow(row)
break
except:
message = "\n*** PYTHON ERRORS *** "; showPyMessage()
message = "Python Traceback Info: " + traceback.format_tb(sys.exc_info()[2])[0]; showPyMessage()
message = "Python Error Info: " + str(sys.exc_type)+ ": " + str(sys.exc_value) + "\n"; showPyMessage()
-
Thanks @EmilyF. So what WAS wrong?FelixIP– FelixIP2015年01月22日 19:38:40 +00:00Commented Jan 22, 2015 at 19:38
arcpy.da.UpdateCursor(fc, ["Name"])
fields list or tuple instead of string.