AnsweredAssumed Answered

Will anything in "with arcpy.da.SearchCursor(fc,fields,'1=2') as cursor" execute?

Question asked by CCStormwater on Mar 8, 2013
Latest reply on Mar 8, 2013 by jimcousins
Does a "with" block always execute even when passed an empty cursor?

For testing purposes I've tried this:

with arcpy.da.SearchCursor(fc, "ST_ID", "1=2") as cursor:     arcpy.AddMessage("In the With")     try:         test = cursor.next()         arcpy.AddError("ERROR:  There is more than one ...")         sys.exit(1)     except StopIteration:         arcpy.AddMessage("All is good, there's one and only one ...")         pass


Note the "where" clause. 

I'm expecting this search cursor to return 0 or 1 rows.  I was thinking that the "with" block would only be executed if I had a row.  Then my intent with the "try" was to catch a 2nd row which I shouldn't ever have.

However in testing this I realized I'm always going into the with block.  To be sure I put in the False where clause.  Even with that where clause I'm seeing the "In the With" statement in my results window. 

I'm trying to wrap my head around the with statement, but even though others have said I have a big head it's not fitting...

PS: or am I not understanding something about the search cursor?

Outcomes