I am try to iterate over 3 feature datasets in 1 gdb and update a field value in all of the feature classes to "No".
ArcMap 10.4.1 Standard license. Windows 7
Here is my code.
import arcpy arcpy.env.workspace = r"PATH HERE" #list of datasets datasets = ["WaterDistribution", "WastewaterSystem", "Stormwater"] #field to update updateField = "AP_CHECKED" #value to enter into field value = "No" for ds in datasets: for fc in arcpy.ListFeatureClasses("","",ds): print("Working on DS: {0}, FC: {1}" .format(ds, fc)) cursor = arcpy.UpdateCursor(fc, updateField) for row in cursor: row.setValue(updateField, value) cursor.updateRow(row)
The code runs in the Idle window and the Datasets and FCs are listed from the print line but none of the values change in the db.
Is the code correct?
Thanks,
James
Solved! Go to Solution.
I was able to get the code to work. Starting an edit session and using arcpy.da.UpdateCursor rather than the arcpy.UpdateCursor seemed to do the trick.
Sorry about the code snippet in the previous post not being formatted correctly. I just figured out how to do that.
import arcpy
arcpy.env.workspace = r"PATH TO GDB HERE"
ws = r"PATH TO GDB HERE"
#list of datasets
datasets = ["WaterDistribution", "WastewaterSystem", "Stormwater"]
#field to update
updateField = "AP_CHECKED"
#value to enter into field
value = "No"
edit = arcpy.da.Editor(ws)
edit.startEditing(False, True)
edit.startOperation()
for ds in datasets:
for fc in arcpy.ListFeatureClasses("","",ds):
print("Working on DS: {0}, FC: {1}" .format(ds, fc))
with arcpy.da.UpdateCursor(fc, updateField) as cursor:
for row in cursor:
row[0] = value
cursor.updateRow(row)
edit.stopOperation()
edit.stopEditing(True)
You will need to format you code using the syntax highlighter (follow the ... 's) so that your indentation can be checked
I was able to get the code to work. Starting an edit session and using arcpy.da.UpdateCursor rather than the arcpy.UpdateCursor seemed to do the trick.
Sorry about the code snippet in the previous post not being formatted correctly. I just figured out how to do that.
import arcpy
arcpy.env.workspace = r"PATH TO GDB HERE"
ws = r"PATH TO GDB HERE"
#list of datasets
datasets = ["WaterDistribution", "WastewaterSystem", "Stormwater"]
#field to update
updateField = "AP_CHECKED"
#value to enter into field
value = "No"
edit = arcpy.da.Editor(ws)
edit.startEditing(False, True)
edit.startOperation()
for ds in datasets:
for fc in arcpy.ListFeatureClasses("","",ds):
print("Working on DS: {0}, FC: {1}" .format(ds, fc))
with arcpy.da.UpdateCursor(fc, updateField) as cursor:
for row in cursor:
row[0] = value
cursor.updateRow(row)
edit.stopOperation()
edit.stopEditing(True)
ArcPy Editor is part of the ArcPy Data Access module, which explains why you have to use the Data Access cursors instead of the older/original ArcPy cursors.