<?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: Setting a script output as model variable in Geoprocessing Questions</title>
    <link>https://community.esri.com/t5/geoprocessing-questions/setting-a-script-output-as-model-variable/m-p/531282#M17699</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I'm pretty sure I've got my answer. Seeing as how I cannot figure out how to create a model variable from a script output using Model Builder, I instead triggered the model using the very same script that generated the variable. Then the variable was used as the model parameter, as follows:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy&amp;nbsp; arcpy.ImportToolbox(r"W:\WDIA_Production\Toolbox.tbx", "TBX")&amp;nbsp; arcpy.ClaimantAddressGeocoding_TBX(variable)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This works perfectly, because I want a script to call the model anyway, so that it can be used within Windows Task Scheduler.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 25 Oct 2013 22:35:14 GMT</pubDate>
    <dc:creator>BenjaminBauman</dc:creator>
    <dc:date>2013-10-25T22:35:14Z</dc:date>
    <item>
      <title>Setting a script output as model variable</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/setting-a-script-output-as-model-variable/m-p/531279#M17696</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello all,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have a script that renames a file with a generic name to one reflecting its week ending date. In addition to the rename process, the script also outputs a string that is to be used as a model variable. This string is the prefix portion of the renamed file name (e.g., clm20130914). If this string can be set as a model variable, then all the remaining steps of the model will name the intermediate and final output files accordingly. The problem is, I cannot seem to assign a script's string output as a string variable within the model. Does anyone have any insight on how to do this? The line containing &lt;/SPAN&gt;&lt;PRE class="plain" name="code"&gt;arcpy.SetParameterAsText(2, i[4])&lt;/PRE&gt;&lt;SPAN&gt; should assign i[4] as the third parameter, correct? I have assigned the third parameter as a derived output. The following is the script for the model's rename portion, and attached is a graphic of the model. Any help would be greatly appreciated. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import datetime,csv,sys,os,arcpy from datetime import date&amp;nbsp; todays_date = datetime.date.today()&amp;nbsp; todays_date = todays_date.strftime("%Y %m %d") todays_date = todays_date.split() todays_date = map(int, todays_date) todays_date = date(todays_date[0], todays_date[1], todays_date[2])&amp;nbsp; calendar = csv.reader(open(sys.argv[1]))&amp;nbsp; calendar.next() calendar.next() calendar.next()&amp;nbsp; l = reversed(list(calendar))&amp;nbsp; for i in l: &amp;nbsp;&amp;nbsp;&amp;nbsp; try: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; file_date = i[0].split('/') &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; file_date = '20' + file_date[2] + ' ' + file_date[0] + ' ' + file_date[1] &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; file_date = file_date.split() &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; file_date = tuple(map(int, file_date)) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; file_date = date(file_date[0], file_date[1], file_date[2]) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if todays_date &amp;gt;= file_date: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; current_file = i[4] + '.ides.csv' &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SetParameterAsText(2, i[4]) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break &amp;nbsp;&amp;nbsp;&amp;nbsp; except IndexError: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pass &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rename_file = os.listdir('W:\WDIA_Production\Geocoding_Files')[0]&amp;nbsp; os.rename(os.path.join(sys.argv[2], rename_file),os.path.join(sys.argv[2],current_file))&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 25 Oct 2013 19:06:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/setting-a-script-output-as-model-variable/m-p/531279#M17696</guid>
      <dc:creator>BenjaminBauman</dc:creator>
      <dc:date>2013-10-25T19:06:54Z</dc:date>
    </item>
    <item>
      <title>Re: Setting a script output as model variable</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/setting-a-script-output-as-model-variable/m-p/531280#M17697</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I would put some debugging code in your python script. so a) you're sure you are getting to that line and b) the value is being set to something you want.&amp;nbsp; My guess as to what is happening here is you are raising an error inside your try block and never get to the line with the SetParameterAsText. Your try block is only handling indexErrors, any other error will just be ignored and processing continues after the try/except.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The practice I usually do is run the SetParameterAsText at the very end, after I know everything ran okay.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;(The repr() function is very handy when you aren't sure exactly what is there, you're guranteed to get an informative string representation of the variable.)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
arcpy.AddMessage("returning {0}".format(repr(i[4]))
arcpy.SetParameterAsText(2, i[4])
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;BTW, your path is specified incorrectly, you need to specify a raw string to avoid issues with back-slashes, in case you specify a folder that begins with the letters t or r:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
rename_file = os.listdir(r'W:\WDIA_Production\Geocoding_Files')[0]
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 23:07:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/setting-a-script-output-as-model-variable/m-p/531280#M17697</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2021-12-11T23:07:56Z</dc:date>
    </item>
    <item>
      <title>Re: Setting a script output as model variable</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/setting-a-script-output-as-model-variable/m-p/531281#M17698</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks for the reply, Curtis. The script works fine outside of the model, so I'm confident that it gets to the line with the return statement. I tried your suggestion though, and placed it outside of the loop, but received the same error message. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;With regard to the line which calls the file, that should actually read sys.argv[1], since it should function as an input parameter. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I've seen suggestions for something related that states placing the Python script in a Calculate Value tool, and am in the midst of trying to create a model variable from that instead. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Another possibility - I want to run the model as a scheduled task by calling it with Python and triggering it routinely through Windows Task Scheduler. Perhaps the Python script that calls the model can also allow input of a model parameter (this being the variable derived from i[4] in the script)? Information for running the model as a scheduled task can be found &lt;/SPAN&gt;&lt;A href="http://joelmccune.com/2011/05/05/run-models-as-scheduled-tasks/"&gt;here&lt;/A&gt;&lt;SPAN&gt;, but there is no mention of feeding in any model parameters - although I'm sure it can be done.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 25 Oct 2013 21:08:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/setting-a-script-output-as-model-variable/m-p/531281#M17698</guid>
      <dc:creator>BenjaminBauman</dc:creator>
      <dc:date>2013-10-25T21:08:21Z</dc:date>
    </item>
    <item>
      <title>Re: Setting a script output as model variable</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/setting-a-script-output-as-model-variable/m-p/531282#M17699</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I'm pretty sure I've got my answer. Seeing as how I cannot figure out how to create a model variable from a script output using Model Builder, I instead triggered the model using the very same script that generated the variable. Then the variable was used as the model parameter, as follows:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy&amp;nbsp; arcpy.ImportToolbox(r"W:\WDIA_Production\Toolbox.tbx", "TBX")&amp;nbsp; arcpy.ClaimantAddressGeocoding_TBX(variable)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This works perfectly, because I want a script to call the model anyway, so that it can be used within Windows Task Scheduler.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 25 Oct 2013 22:35:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/setting-a-script-output-as-model-variable/m-p/531282#M17699</guid>
      <dc:creator>BenjaminBauman</dc:creator>
      <dc:date>2013-10-25T22:35:14Z</dc:date>
    </item>
  </channel>
</rss>

