Skip to main content
3 of 3
Commonmark migration

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

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'"
with arcpy.da.SearchCursor(fc, fields, where) as cursor:
    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.

How should we use next() with arcpy.da.SearchCursor?

Stephen Lead
  • 21.2k
  • 18
  • 114
  • 241