0

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

  • Convert your single quotes on your workspace line to double quotes. – Hornbydd Jan 22 '15 at 15:00
  • 1
    Restart your ArcGIS Desktop(kill all desktop related processes) and try again, some times ArcGIS encounter some glitches like this. Your script is fine, though I suggest use arcpy.da.UpdateCursor(fc, ["Name"]) fields list or tuple instead of string. – Surya Jan 22 '15 at 19:22
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 Jan 22 '15 at 19:38

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.