Select to view content in your preferred language

AttributeError: __exit__

6265
12
Jump to solution
03-14-2018 07:05 AM
VishalShah2
Frequent Contributor

Hello,

So I am new to working with cursors and need a little help with my code as I am getting an AttributeError: __exit__ when i run my script. Here's what my code looks like:

inTable = r'Layer\Fiber'
routeMilesField = "RouteMiles"
strandsField = "cableSize"
milesField = "FiberMiles"
fields = [strandsField, milesField, routeMilesField]


with arcpy.UpdateCursor(fiberFeatures, fields) as cursor:
    for row in cursor:
        if None in row[:-1]:
            arcpy.AddMessage("Insufficient Strand Data!")
            break
        else:
            row[2] = row[0] * row[1]
            cursor.updateRow(row)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Any help is greatly appreciated!

Edit:

Revised script based on this article: cursor - AttributeError: __exit__ when trying to use arcpy.UpdateCursor - Geographic Information Sys... 

cursor = arcpy.UpdateCursor(fiberFeatures, fields)
for row in cursor:
    if None in row[:-1]:
        arcpy.AddMessage("Insufficient Strand Data")
        break
    else:
        row[2] = row[0] * row[1]
        cursor.updateRow(row)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

This returns a TypeError: 'Row' object has no attribute '__getitem__' error. I'm new to cursors and what not and definitely need help with this code.

Edit 2: Solution based on Joshua's feedback:

with arcpy.da.UpdateCursor(fiberFeatures, fields) as cursor:
    for row in cursor:
        if None in row[:-1]:
            arcpy.AddError("Insufficient Strand Data!")
            break
        else:
            row[2] = row[0] * row[1]
            cursor.updateRow(row)
0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

Instead of investing in fixing code that uses the old ArcPy cursors, I recommend spending a few minutes to read up on the not-so-new ArcPy Data Access cursors.  They are much more efficient, and a bit more Pythonic, than the original Python cursors.  UpdateCursor—Help | ArcGIS Desktop 

View solution in original post

12 Replies
RebeccaStrauch__GISP
MVP Emeritus

You are missing a quote at the end of the first line.  See if that makes a difference

inTable = r'Layer\Fiber'
0 Kudos
VishalShah2
Frequent Contributor

Apologies Rebecca, my code actually has the quote at the end. I forgot to type that in. But that doesn't seem to cause the error. Any other ideas?

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

Ah yes, typos happen.  I saw the missing quote and didn't go beyond that since need to fix the obvious errors first.   Looks like you have it resolved now.

VishalShah2
Frequent Contributor

Thank you for the help! It was greatly appreciated!

0 Kudos
DanPatterson_Retired
MVP Emeritus

it is hard to provide an example without creating a class or running a file, but have you considered reversing your condition or do you want to completely bail?

if not(None in row[:-1]):
    row[2] = row[0] * row[1]
    cursor.updateRow(row)
0 Kudos
VishalShah2
Frequent Contributor

I am open to anything Dan as long as it does what I need it to do. The path I am taking is to sort through the strandsField to first see if there are any NULL values, if there are any, return a message saying Insufficient Data. If there are no NULL values, then multiple the strandsField with the milesField. But I want the first conditional to run to inform users if there is insufficient data. Hopefully that makes sense!

0 Kudos
VishalShah2
Frequent Contributor

Dan, I just tried it with the code you had listed but within the with statement:

with arcpy.UpdateCursor(fiberFeatures, fields) as cursor:
    if not(None in row[:-1]):
        row[2] = row[0] * row[1]
        cursor.updateRow(row)

This still gives me the AttributeError: __Exit__

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Instead of investing in fixing code that uses the old ArcPy cursors, I recommend spending a few minutes to read up on the not-so-new ArcPy Data Access cursors.  They are much more efficient, and a bit more Pythonic, than the original Python cursors.  UpdateCursor—Help | ArcGIS Desktop 

VishalShah2
Frequent Contributor

That did the trick Joshua. Changing it from arcpy.UpdateCursor to arcpy.da.UpdateCursor.

0 Kudos