AnsweredAssumed Answered

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

Question asked by stevel on Apr 30, 2014
Latest reply on Apr 30, 2014 by stevel
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

Outcomes