rows = arcpy.da.SearchCursor(someFC,someFields,someQuery) print (len(rows)) #I'm sure this used to work with the old cursors, but doesn't work with da.SearchCursor
Solved! Go to Solution.
arcpy.MakeFeatureLayer_management(fc, "fl1") arcpy.SelectLayerByAttribute_management("fl1", "NEW_SELECTION", "LAND_COVER_CD in (41,42)") result = len([r[0] for r in arcpy.da.SearchCursor("fl1", ["OID@"])]) #Or this if you don't need an actual "selected set"... result = len([r[0] for r in arcpy.da.SearchCursor(fc, ["OID@"], "LAND_COVER_CD in (41,42)")])
I'd suggest you to encapsulate it into a function. I developed a tiny function called SearchCounter derivated from a Validator in a previous blog post.
def SearchCounter(cursor):
counts = 0
for counter in cursor:
counts += 1;
cursor.reset()
return counts
#Previous Code
#...
RecordCount = SearchCounter(arcpy.da.SearchCursor(Table,Fields,Query))
#...
#End Code
Greetings
This also now works:
with arcpy.da.SearchCursor(table, fields, query) as cursor:
rows = list(cursor)
row_count = len(rows)
...though you would then need to re-create the cursor if you want to iterate over the rows for something else.