<?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: ArcPy: End script if condition is true... in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/arcpy-end-script-if-condition-is-true/m-p/363732#M28773</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Are you &lt;EM&gt;sure&lt;/EM&gt; that your data = 0? Try setting pHx10 = 0 before the if statement, and see if the script terminates.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Otherwise, post your complete script, showing how you set pHx10.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;As for the else statement, you're not required to provide an else, at all, if you don't want to do something.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 15 Sep 2015 21:22:07 GMT</pubDate>
    <dc:creator>DarrenWiens2</dc:creator>
    <dc:date>2015-09-15T21:22:07Z</dc:date>
    <item>
      <title>ArcPy: End script if condition is true...</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-end-script-if-condition-is-true/m-p/363731#M28772</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;PythonWin; ArcGIS 10.3&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm trying to implement an exit function that was posted on another site (&lt;A href="http://gis.stackexchange.com/questions/1015/how-to-have-a-python-script-self-terminate-in-arcmap10" title="http://gis.stackexchange.com/questions/1015/how-to-have-a-python-script-self-terminate-in-arcmap10"&gt;http://gis.stackexchange.com/questions/1015/how-to-have-a-python-script-self-terminate-in-arcmap10&lt;/A&gt;), but it's not working...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;# Create function to exit script&lt;/P&gt;&lt;P&gt;def finish(arg=None):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; try:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sys.exit(arg)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; except SystemExit:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pass&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;# Check for nodata (Note: Shapefiles don't support nodata - ArcGIS turns them into zeroes)&lt;/P&gt;&lt;P&gt;if pHx10 == 0:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "The point you selected is water or otherwise undefined"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; finish()&lt;/P&gt;&lt;P&gt;else:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Point's data is defined"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;When I input a point that's associated with nodata (i.e. pHx10==0)... the script doesn't exit. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Also... there must be a way to end the else statement without using a print statement. Break and continue are used in while and for statements. So what do you use for an if statement?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm new to ArcGIS and don't know Python.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 15 Sep 2015 21:15:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-end-script-if-condition-is-true/m-p/363731#M28772</guid>
      <dc:creator>MaribethMilner</dc:creator>
      <dc:date>2015-09-15T21:15:07Z</dc:date>
    </item>
    <item>
      <title>Re: ArcPy: End script if condition is true...</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-end-script-if-condition-is-true/m-p/363732#M28773</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Are you &lt;EM&gt;sure&lt;/EM&gt; that your data = 0? Try setting pHx10 = 0 before the if statement, and see if the script terminates.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Otherwise, post your complete script, showing how you set pHx10.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;As for the else statement, you're not required to provide an else, at all, if you don't want to do something.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 15 Sep 2015 21:22:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-end-script-if-condition-is-true/m-p/363732#M28773</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2015-09-15T21:22:07Z</dc:date>
    </item>
    <item>
      <title>Re: ArcPy: End script if condition is true...</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-end-script-if-condition-is-true/m-p/363733#M28774</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Yes. I use the ExtractMultiValuesToPoints function to extract the data to a shapefile and was surprised to find zero values for each raster that had nodata at the point I entered.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I originally typed if pHx10 = 0, and PythonWin wouldn't run the script until I set it to pHx10 == 0.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Just tried if pHx10 &amp;lt; 1. Still didn't work.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here's the code...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;import arcpy&lt;/P&gt;&lt;P&gt;from arcpy.sa import *&lt;/P&gt;&lt;P&gt;arcpy.CheckOutExtension("Spatial")&lt;/P&gt;&lt;P&gt;from arcpy import env&lt;/P&gt;&lt;P&gt;# set environment&lt;/P&gt;&lt;P&gt;env.workspace = "D:/.../Data"&lt;/P&gt;&lt;P&gt;env.scratchWorkspace = "D:/...Data"&lt;/P&gt;&lt;P&gt;env.scratchWorkspace = env.scratchFolder&lt;/P&gt;&lt;P&gt;env.overwriteOutput = True&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;# Create function to exit script&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;# &lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=http%3A%2F%2Fgis.stackexchange.com%2Fquestions%2F1015%2Fhow-to-have-a-python-script-self-terminate-in-arcmap10" rel="nofollow" target="_blank"&gt;http://gis.stackexchange.com/questions/1015/how-to-have-a-python-script-self-terminate-in-arcmap10&lt;/A&gt;&lt;/P&gt;&lt;P&gt;def finish(arg=None):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; try:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sys.exit(arg)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; except SystemExit:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pass&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;# Create point file (eventually from user inputs)&lt;/P&gt;&lt;P&gt;arcpy.CreateFeatureclass_management("D:/.../Data/Out", "selpoint.shp", "POINT", "", "DISABLED", "DISABLED", "D:/.../Data/Data8-26-15-0001.shp")&lt;/P&gt;&lt;P&gt;fc = "D:/.../Data/Out/selpoint.shp"&lt;/P&gt;&lt;P&gt;cursor = arcpy.da.InsertCursor(fc, ["SHAPE@XY"])&lt;/P&gt;&lt;P&gt;xy = (31.9, 0.34)&lt;/P&gt;&lt;P&gt;cursor.insertRow([xy])&lt;/P&gt;&lt;P&gt;del cursor&lt;/P&gt;&lt;P&gt;del xy&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;# Extract grid values associated with the point&lt;/P&gt;&lt;P&gt;ExtractMultiValuesToPoints(fc, [["aikm", "aikm"], ["bio4km", "bio4km"], ["avggddkm", "avggddkm"], ["soc250", "soc250"], ["ph250x10", "ph250x10"], ["sand250", "sand250"], ["dem500", "dem500"], ["latdx100", "latdx100"]],"NONE")&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;# Assign extracted values to variables; Note: latdx100 = |latitude| x 100&lt;/P&gt;&lt;P&gt;cursor = arcpy.SearchCursor(fc)&lt;/P&gt;&lt;P&gt;for row in cursor:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ai = row.getValue('aikm')&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; bio4 = row.getValue('bio4km')&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; soc = row.getValue('soc250')&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; pHx10 = row.getValue('ph250x10')&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sand = row.getValue('sand250')&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; dem = row.getValue('dem500')&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; latx100 = row.getValue('latdx100')&lt;/P&gt;&lt;P&gt;del cursor&lt;/P&gt;&lt;P&gt;del row&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;# Check for nodata. Nodata isn't supported in shapefiles. Becomes zero.&lt;/P&gt;&lt;P&gt;if pHx10 &amp;lt; 1:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "The point you selected is water or otherwise undefined."&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "To identify a new point near your target area..."&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "a) In ArcMap... turn on soc250."&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "b) Use the Go To XY and zoom tools to zoom into your target,"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "&amp;nbsp;&amp;nbsp; position your cursor over data near your target area"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "&amp;nbsp;&amp;nbsp; and note the location in the status bar (bottom)"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; finish()&lt;/P&gt;&lt;P&gt;else:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Point's data is defined"&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 15 Sep 2015 21:32:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-end-script-if-condition-is-true/m-p/363733#M28774</guid>
      <dc:creator>MaribethMilner</dc:creator>
      <dc:date>2015-09-15T21:32:56Z</dc:date>
    </item>
    <item>
      <title>Re: ArcPy: End script if condition is true...</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-end-script-if-condition-is-true/m-p/363734#M28775</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The &lt;A href="https://docs.python.org/2/library/sys.html"&gt;Python sys &lt;/A&gt;documentation explains what is going on:&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;sys.&lt;STRONG&gt;exit&lt;/STRONG&gt;([arg])&lt;/P&gt;&lt;P&gt;Exit from Python. This is implemented by raising the SystemExit exception, so cleanup actions specified by finally clauses of try statements are honored, and it is possible to intercept the exit attempt at an outer level.&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;Since exit() ultimately “only” raises an exception, it will only exit the process when called from the main thread, and the exception is not intercepted.&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;You are raising the SystemExit exception, promptly trapping it, and then ignoring it with your pass statement.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 15 Sep 2015 21:41:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-end-script-if-condition-is-true/m-p/363734#M28775</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2015-09-15T21:41:34Z</dc:date>
    </item>
    <item>
      <title>Re: ArcPy: End script if condition is true...</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-end-script-if-condition-is-true/m-p/363735#M28776</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;why arent you passing 0 to finish? ie finish(0) instead of finish&lt;/P&gt;&lt;P&gt;you should alway set up your condition checking in the postive where possible. for example&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def finish(arg=None):
&amp;nbsp;&amp;nbsp;&amp;nbsp; if arg:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print("\nargument is {} and I can carry on".format(arg))
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print("\nargument is {} and I should quit".format(arg))
&amp;nbsp;&amp;nbsp;&amp;nbsp; return arg&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;for i in [0,1,None,'a',2]:
&amp;nbsp;&amp;nbsp;&amp;nbsp; result=finish(i)
&amp;nbsp;&amp;nbsp;&amp;nbsp; print("made it here with...{}".format(result))&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;results in the following&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;argument is 0 and I should quit
made it here with...0

argument is 1 and I can carry on
made it here with...1

argument is None and I should quit
made it here with...None

argument is a and I can carry on
etc &lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;when you run finish without arguments...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; finish()
argument is None and I should quit&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 16:57:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-end-script-if-condition-is-true/m-p/363735#M28776</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2021-12-11T16:57:05Z</dc:date>
    </item>
    <item>
      <title>Re: ArcPy: End script if condition is true...</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-end-script-if-condition-is-true/m-p/363736#M28777</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm new to this site... as well as ArcPy (which is what I meant to say in the first post)... &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;...and this is the only way I could find to complete this thread.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;1) I didn't import sys&lt;/P&gt;&lt;P&gt;2) The original link that I cited had a secondary link to an ESRI example...&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 11.0pt; font-family: 'Calibri',sans-serif;"&gt;&lt;A href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//000v000000vp000000.htm"&gt;http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//000v000000vp000000.htm&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 11.0pt; font-family: 'Calibri',sans-serif;"&gt;Turns out I didn't need the function. Just needed to add sys.exit(0) (without the else statement - thanks for that)...&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;# Check for nodata. Nodata isn't supported in shapefiles. Becomes zero.&lt;/P&gt;&lt;P&gt;if pHx10 == 0:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "The point you selected is water or otherwise undefined."&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "To identify a new point near your target area..."&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "a) In ArcMap... turn on soc250."&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "b) Use the Go To XY and zoom tools to zoom into your target,"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "&amp;nbsp;&amp;nbsp; position your cursor over data near your target area"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "&amp;nbsp;&amp;nbsp; and note the location in the status bar (bottom)"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sys.exit(0)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "didn't exit"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 11.0pt; font-family: 'Calibri',sans-serif;"&gt;All is well.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 16 Sep 2015 17:46:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-end-script-if-condition-is-true/m-p/363736#M28777</guid>
      <dc:creator>MaribethMilner</dc:creator>
      <dc:date>2015-09-16T17:46:58Z</dc:date>
    </item>
  </channel>
</rss>

