1

I'm trying to populate a field from values of an existing field in a feature class using arcpy.UpdateCursor.

I'm getting this error: AttributeError: __exit__

Here's the chunk of code I'm using:

with arcpy.UpdateCursor(polyline, ["SEWER_TYPE"]) as cursor:
    for row in cursor:
        if row.getValue("Layer") == "FUTURE_STORM":
            row.setValue("SEWER_TYPE", "STORM")
            cursor.updateRow(row)
del row, cursor
4

You do not need to delete the row/cursor objects when using the with statement, this will automatically clean up when __exit__ is called after the cursor has exhausted itself.

Also, it is easier to reference the fields by index because the rows are returned as tuples:

with arcpy.da.UpdateCursor(polyline, ["SEWER_TYPE", "Layer"]) as cursor:
    for row in cursor:
        if row[1] == "FUTURE_STORM":
            row[0] = "STORM"
            cursor.updateRow(row)

Because you are using the with statement, I am assuming you are using at least ArcGIS 10.1 or beyond? If not, maybe that is why you are getting an attribute area because it does not have the __exit__ method. In this case, do not use a with statement:

cursor = arcpy.UpdateCursor(polyline)
for row in cursor:
    if row.getValue("Layer") == "FUTURE_STORM":
        row.setValue("SEWER_TYPE", "STORM")
        cursor.updateRow(row)
del row, cursor
  • 1
    Thanks a so much ! Really appreciate it! It works! I'm using 10.2 and I got some conflicting info on which update cursor to use (arcy.da.UpdateCursor vs arcpy.UpdateCursor). Thanks again! – Kurt Cederholm May 19 '15 at 22:27

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.