Select to view content in your preferred language

Why does the next() method fail after the last record, using arcpy.da.SearchCursor?

2686
10
Jump to solution
04-30-2014 03:40 PM
StephenLead
Honored Contributor
Using the older arcpy.SearchCursor, the next() method would return Nothing once the last record had been passed.

Using the newer arcpy.da.SearchCursor, I'm finding that the next() method crashes the script if there are no more records. To illustrate:

import arcpy   fc = "C:\Program Files (x86)\ArcGIS\Desktop10.2\ArcGlobeData\continent.shp" fields = ["CONTINENT"] where = "CONTINENT = 'Asia'" cursor = arcpy.da.SearchCursor(fc, fields, where) for i in range(0,5):     row = cursor.next()     print(row)


There is only one record in the cursor, so the first time it iterates the name is printed. But the second iteration throws a StopIteration error.

I believe this is a bug - the cursor should return Nothing, allowing us to handle the situation. At the very least, this behaviour should be documented.

(I know there is an alternative method using for row in cursor: but my point is that the next() method should not throw an error after the last record. Also, in my case I'm looking for situations where there are no records so I can perform an action - this doesn't work when using for row in cursor:).

Thanks,
Steve
Tags (2)
10 Replies
StephenLead
Honored Contributor
Thanks Jason. Could you add this information to the help file entries for SearchCursor, InsertCursor and UpdateCursor?
0 Kudos