I wrote a script that is intended to find the highest currently existing feature ID and then increment any features with NULL ID's by 1. To do this, I had to add some temporary fields to a feature class. Now, in my 'finally:' block I'm trying to delete these temporary fields but I keep getting the following error:<class 'arcgisscripting.ExecuteError'>: ERROR 000496: table is being editedFailed to execute (DeleteField).I did make use of search and update cursors, and I assume these are not being deleted properly and causing a file lock. I'm pretty new to Python, so I guess I don't know how to go about deleting them properly. What I've done so far is based off of what I found in the ArcGIS help documentation. Can anyone clear this issue up for me? Thanks!import arcpy
try:
MXD = arcpy.mapping.MapDocument("CURRENT")
# Get selection feature class from map document
FLList = arcpy.mapping.ListLayers(MXD, "*swDischarge*")[0]
# Get 'select by' feature class from map document
QSList = arcpy.mapping.ListLayers(MXD, "*Quarter*")[0]
arcpy.AddMessage("Feature layers obtained.")
# Create search cursor and iterate to first (and only) record
QSCur = arcpy.SearchCursor(QSList, "", "", "GRIDID")
QSRow = QSCur.next()
# Get field value of current record
QSValue = QSRow.getValue("GRIDID")
arcpy.AddMessage("Quarter section value obtained.")
# Select all features within selected quarter section
arcpy.SelectLayerByLocation_management(FLList, "INTERSECT", QSList)
arcpy.AddMessage("Features in quarter section selected.")
# Add fields to store calulated ID info.
arcpy.AddField_management(FLList, "QS", "TEXT", "", "", 4)
arcpy.AddField_management(FLList, "IDCODE", "TEXT", "", "", 3)
# Add field to store existing ID number sequence
arcpy.AddField_management(FLList, "SEQCODEINT", "SHORT", 3)
# Add field to store ID number converted to text
arcpy.AddField_management(FLList, "SEQCODETXT", "TEXT", 3)
arcpy.AddMessage("Temporary fields added.")
# Calculate SEQCODEINT field to extract number sequence
arcpy.CalculateField_management(FLList, "SEQCODEINT", "Right([FACILITYID], 3)", "VB")
# Create search cursor and iterate to first (and highest) field value
SeqCur = arcpy.SearchCursor(FLList, "", "", "SEQCODEINT", "SEQCODEINT D")
SeqRow = SeqCur.next()
# Get field value of current record
SeqValue = SeqRow.getValue("SEQCODEINT")
arcpy.AddMessage("Highest ID value obtained.")
# Select from current selection all records with NULL facility identifiers
arcpy.SelectLayerByAttribute_management(FLList, "SUBSET_SELECTION", "FACILITYID IS NULL")
arcpy.AddMessage("Features with NULL ID values selected.")
# Create update cursor
FullIDCurUp = arcpy.UpdateCursor(FLList)
for FullIDRowUp in FullIDCurUp:
# Increment ID by 1 each pass through
SeqValue = SeqValue + 1
# Convert ID number to text and pad with zero's
TxtSeqValue = str(SeqValue)
TxtSeqValueFill = TxtSeqValue.zfill(3)
# Calculate new fields
FullIDRowUp.QS = QSValue
FullIDRowUp.IDCODE = "STO"
FullIDRowUp.SEQCODEINT = SeqValue
FullIDRowUp.SEQCODETXT = TxtSeqValueFill
# Update records with new values
FullIDCurUp.updateRow(FullIDRowUp)
arcpy.AddMessage("Temporary field values updated.")
# Create update cursor
IDCurUp = arcpy.UpdateCursor(FLList)
for IDRowUp in IDCurUp:
# Combine new field values and update FACILITYID
IDRowUp.FACILITYID = IDRowUp.QS + IDRowUp.IDCODE + IDRowUp.SEQCODETXT
IDCurUp.updateRow(IDRowUp)
arcpy.AddMessage("Facility Identifier Update Successful!")
# Get tool errors
except arcpy.ExecuteError:
arcpy.AddError(arcpy.GetMessages(2))
# Get all other errors
except Exception as e:
print e.message
arcpy.AddError(e.message)
finally:
# Delete cursors to prevent file lock.
if QSCur:
del QSCur
if QSRow:
del QSRow
if QSValue:
del QSValue
if SeqCur:
del SeqCur
if SeqRow:
del SeqRow
if SeqValue:
del SeqValue
if FullIDCurUp:
del FullIDCurUp
if FullIDRowUp:
del FullIDRowUp
if IDCurUp:
del IDCurUp
if IDRowUp:
del IDRowUp
arcpy.AddMessage("Cursors deleted")
# Delete temporary fields
arcpy.DeleteField_management(FLList, "QS")
arcpy.DeleteField_management(FLList, "IDCODE")
arcpy.DeleteField_management(FLList, "SEQCODEINT")
arcpy.DeleteField_management(FLList, "SEQCODETXT")
arcpy.AddMessage("Temporary fields deleted.")