<?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: If Then Else in model builder in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/if-then-else-in-model-builder/m-p/367074#M28951</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Could also just check to see if file exists, and size:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;

import os, stat

if os.path.isfile(infile):&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # checks if exists, otherwise get error if checking size on non-exist file
 os.stat(infile)[stat.ST_SIZE]&amp;nbsp;&amp;nbsp; ##&amp;nbsp; returns size of file&amp;nbsp; 0L if empty
else:
 print "file not exist"

&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This is done pretty much instantly..&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;R_&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 17:03:14 GMT</pubDate>
    <dc:creator>RhettZufelt</dc:creator>
    <dc:date>2021-12-11T17:03:14Z</dc:date>
    <item>
      <title>If Then Else in model builder</title>
      <link>https://community.esri.com/t5/python-questions/if-then-else-in-model-builder/m-p/367071#M28948</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi all,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I have a model which iterates through feature classes in a geodatabase (image attached)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;For each feature class, it runs the check Geometry tool.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I then use the "Get Count" tool to count the number of rows in the output table.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I then want to add an expression where if the number of rows is greater than 0 it should keep running the rest of the model, otherwise it should go on to the next feature class.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I thought this could be achieved by using the Calculate Value tool using the following code.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Expression&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;CountMe(%Row Count%)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Code Block&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;def CountMe(n):
 import arcpy
 if n &amp;gt; 0:
&amp;nbsp; return "true"
 else:
&amp;nbsp; return "false"&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Where %Row Count% is the output from the "Get Count" tool.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This returns True and False values, and I have the output from this set as a precondition to the next steps in the model.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;However, I assumed that only "true" data would go through, but all the data seems to go through i.e. the model will push through an empty table from the Check Geometry tool all the way to the end.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I've had a read online and I think that using the Calculate Value tool is the wrong way to go about this.&amp;nbsp; Am I right in assuming that what I need is a python script that will give me 2 outputs (True and False) and then I can connect the "True" as a precondition to the rest of the model, and leave the "False" disconnected, so the model stops and the iterator goes on to the next feature class?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If so, would anybody be able to give me some help with this?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Many thanks&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Dan&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 14 Aug 2013 08:49:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/if-then-else-in-model-builder/m-p/367071#M28948</guid>
      <dc:creator>DanielHall_Ballester</dc:creator>
      <dc:date>2013-08-14T08:49:43Z</dc:date>
    </item>
    <item>
      <title>Re: If Then Else in model builder</title>
      <link>https://community.esri.com/t5/python-questions/if-then-else-in-model-builder/m-p/367072#M28949</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I don't know for sure, but a quick thing to try might be using actual True and False Python values, i.e.:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Code Block&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def CountMe(n):
 if n &amp;gt; 0:
&amp;nbsp; return True
 else:
&amp;nbsp; return False&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Also, you do not need to "import arcpy" within the code, this is done automatically.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 17:03:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/if-then-else-in-model-builder/m-p/367072#M28949</guid>
      <dc:creator>StacyRendall1</dc:creator>
      <dc:date>2021-12-11T17:03:09Z</dc:date>
    </item>
    <item>
      <title>Re: If Then Else in model builder</title>
      <link>https://community.esri.com/t5/python-questions/if-then-else-in-model-builder/m-p/367073#M28950</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I just did something similar to this yesterday. I have a test model and a code that might be a good reference. I just created 2 tables with different row counts to test and used a model parameters as input :&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
rowcount = int(arcpy.GetParameterAsText(0))

if rowcount &amp;gt;= 1:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SetParameterAsText(1, "True")
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SetParameterAsText(2, "False")
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Rows Exist, updating table")
elif rowcount == 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SetParameterAsText(1, "False")
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SetParameterAsText(2, "True")
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("0 rows, updating table")
else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Code Didn't Work")
del rowcount
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 17:03:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/if-then-else-in-model-builder/m-p/367073#M28950</guid>
      <dc:creator>AmyKlug</dc:creator>
      <dc:date>2021-12-11T17:03:12Z</dc:date>
    </item>
    <item>
      <title>Re: If Then Else in model builder</title>
      <link>https://community.esri.com/t5/python-questions/if-then-else-in-model-builder/m-p/367074#M28951</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Could also just check to see if file exists, and size:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;

import os, stat

if os.path.isfile(infile):&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # checks if exists, otherwise get error if checking size on non-exist file
 os.stat(infile)[stat.ST_SIZE]&amp;nbsp;&amp;nbsp; ##&amp;nbsp; returns size of file&amp;nbsp; 0L if empty
else:
 print "file not exist"

&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This is done pretty much instantly..&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;R_&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 17:03:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/if-then-else-in-model-builder/m-p/367074#M28951</guid>
      <dc:creator>RhettZufelt</dc:creator>
      <dc:date>2021-12-11T17:03:14Z</dc:date>
    </item>
    <item>
      <title>Re: If Then Else in model builder</title>
      <link>https://community.esri.com/t5/python-questions/if-then-else-in-model-builder/m-p/367075#M28952</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;I just did something similar to this yesterday. I have a test model and a code that might be a good reference. I just created 2 tables with different row counts to test and used a model parameters as input :&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
rowcount = int(arcpy.GetParameterAsText(0))

if rowcount &amp;gt;= 1:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SetParameterAsText(1, "True")
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SetParameterAsText(2, "False")
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Rows Exist, updating table")
elif rowcount == 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SetParameterAsText(1, "False")
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SetParameterAsText(2, "True")
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("0 rows, updating table")
else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Code Didn't Work")
del rowcount
&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This is close to the solution that I'm looking for.&amp;nbsp; I'm "simply" attempting to iterate through my outputs of a process, and when there is an empty geography created it, to delete it.&amp;nbsp; How did you get the outputs from this script to then link back to the geoprocessing tools? i.e. I have embedded this revised script into modelbuilder, but now am stuck on how to connect the Delete tool to the process when the rowcount == 0.&amp;nbsp; Thanks in advance for any assistance.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 17:03:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/if-then-else-in-model-builder/m-p/367075#M28952</guid>
      <dc:creator>AlisterFenix</dc:creator>
      <dc:date>2021-12-11T17:03:17Z</dc:date>
    </item>
  </channel>
</rss>

