arcpy.da.searchcursor with nothing selected returns all records

4270
1
Jump to solution
01-27-2015 01:52 PM
BrianBolduc
New Contributor

I am trying to loop thru selected features using the da.SearchCursor statement below.  It works when the users selects something.  When there are no points or lines selected it returns the whole layer.  Should I be using another function?

       with arcpy.da.SearchCursor(manhole_layer,"*") as cursor:

            for row in cursor:

0 Kudos
1 Solution

Accepted Solutions
BlakeTerhune
MVP Regular Contributor

Before opening the search cursor, could you check to see if any features are selected? If there are selected features, continue to use search cursor; If not, handle exception.

Assuming it is a layer, here's the direction I would go.

I have not tested this. You may have to check for a length like len(desc.FIDSet) > 0

desc = arcpy.Describe(manhole_layer)
if desc.FIDSet:
    # Do something with selection
    with arcpy.da.SearchCursor(manhole_layer, "*") as cursor:
        for row in cursor:
            print row
else:
    # Handle Exception
    print "No features selected."

View solution in original post

1 Reply
BlakeTerhune
MVP Regular Contributor

Before opening the search cursor, could you check to see if any features are selected? If there are selected features, continue to use search cursor; If not, handle exception.

Assuming it is a layer, here's the direction I would go.

I have not tested this. You may have to check for a length like len(desc.FIDSet) > 0

desc = arcpy.Describe(manhole_layer)
if desc.FIDSet:
    # Do something with selection
    with arcpy.da.SearchCursor(manhole_layer, "*") as cursor:
        for row in cursor:
            print row
else:
    # Handle Exception
    print "No features selected."