<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Will anything in &amp;quot;with arcpy.da.SearchCursor(fc,fields,'1=2') as cursor&amp;quot; execute? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/will-anything-in-amp-amp-quot-with-arcpy-da/m-p/38433#M3016</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;If you don't expect a lot of rows, GetCount would probably be very quick for this test:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
numRows = int(arcpy.GetCount_management(fc).getOutput(0))
if numRows == 1:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("All is good, there's one and only one ...")
else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError("All is not good: {0} rows found".format(numRows))
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This worked for me too:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
with arcpy.da.SearchCursor(fc, "ST_ID") as cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("In the With")
&amp;nbsp;&amp;nbsp;&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; test = cursor.next()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Got one.")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; test = cursor.next() # try get a second row
&amp;nbsp;&amp;nbsp;&amp;nbsp; except StopIteration:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError("ERROR:&amp;nbsp; There is more than one ...")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sys.exit(1)
&amp;nbsp;&amp;nbsp;&amp;nbsp; except:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError("Something else happened!")
 &lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 10 Dec 2021 21:29:52 GMT</pubDate>
    <dc:creator>curtvprice</dc:creator>
    <dc:date>2021-12-10T21:29:52Z</dc:date>
    <item>
      <title>Will anything in &amp;amp;quot;with arcpy.da.SearchCursor(fc,fields,'1=2') as cursor&amp;amp;quot; execute?</title>
      <link>https://community.esri.com/t5/python-questions/will-anything-in-amp-amp-quot-with-arcpy-da/m-p/38432#M3015</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Does a "with" block always execute even when passed an empty cursor?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;For testing purposes I've tried this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;with arcpy.da.SearchCursor(fc, "ST_ID", "1=2") as cursor: &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("In the With") &amp;nbsp;&amp;nbsp;&amp;nbsp; try: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; test = cursor.next() &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError("ERROR:&amp;nbsp; There is more than one ...") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sys.exit(1) &amp;nbsp;&amp;nbsp;&amp;nbsp; except StopIteration: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("All is good, there's one and only one ...") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pass&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Note the "where" clause.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm expecting this search cursor to return 0 or 1 rows.&amp;nbsp; I was thinking that the "with" block would only be executed if I had a row.&amp;nbsp; Then my intent with the "try" was to catch a 2nd row which I shouldn't ever have. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;However in testing this I realized I'm always going into the with block.&amp;nbsp; To be sure I put in the False where clause.&amp;nbsp; Even with that where clause I'm seeing the "In the With" statement in my results window.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;PS: or am I not understanding something about the search cursor?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 08 Mar 2013 16:14:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/will-anything-in-amp-amp-quot-with-arcpy-da/m-p/38432#M3015</guid>
      <dc:creator>StormwaterWater_Resources</dc:creator>
      <dc:date>2013-03-08T16:14:40Z</dc:date>
    </item>
    <item>
      <title>Re: Will anything in "with arcpy.da.SearchCursor(fc,fields,'1=2') as cursor" execute?</title>
      <link>https://community.esri.com/t5/python-questions/will-anything-in-amp-amp-quot-with-arcpy-da/m-p/38433#M3016</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;If you don't expect a lot of rows, GetCount would probably be very quick for this test:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
numRows = int(arcpy.GetCount_management(fc).getOutput(0))
if numRows == 1:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("All is good, there's one and only one ...")
else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError("All is not good: {0} rows found".format(numRows))
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This worked for me too:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
with arcpy.da.SearchCursor(fc, "ST_ID") as cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("In the With")
&amp;nbsp;&amp;nbsp;&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; test = cursor.next()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Got one.")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; test = cursor.next() # try get a second row
&amp;nbsp;&amp;nbsp;&amp;nbsp; except StopIteration:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError("ERROR:&amp;nbsp; There is more than one ...")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sys.exit(1)
&amp;nbsp;&amp;nbsp;&amp;nbsp; except:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError("Something else happened!")
 &lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 21:29:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/will-anything-in-amp-amp-quot-with-arcpy-da/m-p/38433#M3016</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2021-12-10T21:29:52Z</dc:date>
    </item>
    <item>
      <title>Re: Will anything in "with arcpy.da.SearchCursor(fc,fields,'1=2') as cursor" execute?</title>
      <link>https://community.esri.com/t5/python-questions/will-anything-in-amp-amp-quot-with-arcpy-da/m-p/38434#M3017</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Yes, a with statement will always step in, attempt the try, and move into the except as necessary. For a deeper explanation: &lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="http://effbot.org/zone/python-with-statement.htm" rel="nofollow" target="_blank"&gt;http://effbot.org/zone/python-with-statement.htm&lt;/A&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 08 Mar 2013 16:59:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/will-anything-in-amp-amp-quot-with-arcpy-da/m-p/38434#M3017</guid>
      <dc:creator>JimCousins</dc:creator>
      <dc:date>2013-03-08T16:59:34Z</dc:date>
    </item>
  </channel>
</rss>

