<?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 make Parameter to be both input/output in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/arcpy-make-parameter-to-be-both-input-output/m-p/418883#M32893</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;That's a bit of an inspired solution.&amp;nbsp; It's silly that ESRI doesn't allow you to append to an output and you can't select an input that doesn't exist.&amp;nbsp; But doing this allows it to work well.&amp;nbsp; I just wanted to add to this with some code.&amp;nbsp; Here's how I implemented that Validation code:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;def __init__(self):&lt;BR /&gt;&amp;nbsp; &amp;nbsp; """Setup arcpy and the list of tool parameters."""&lt;BR /&gt;&amp;nbsp; &amp;nbsp; self.params = arcpy.GetParameterInfo()&lt;BR /&gt;&amp;nbsp; &amp;nbsp; self.params[2].enabled = False&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;def updateParameters(self):&lt;BR /&gt;&amp;nbsp; &amp;nbsp; """Modify the values and properties of parameters before internal validation is performed. This method is called whenever a parameter has been changed."""&lt;BR /&gt;&amp;nbsp; &amp;nbsp; if self.params[1].altered:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if not self.params[1].value is None:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if arcpy.Exists(self.params[1].value):&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; self.params[2].value = self.params[1].valueAsText&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; self.params[1].value = None&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; else:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; self.params[2].value = None&lt;BR /&gt;&amp;nbsp; &amp;nbsp; return&lt;/P&gt;&lt;P&gt;def updateMessages(self):&lt;BR /&gt;&amp;nbsp; &amp;nbsp; """Modify the messages created by internal validation for each tool parameter. This method is called after internal validation."""&lt;BR /&gt;&amp;nbsp; &amp;nbsp; if self.params[1].value is None and self.params[2].value is None:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; self.params[1].setErrorMessage("Must enter a value")&lt;BR /&gt;&amp;nbsp; &amp;nbsp; else:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; self.params[1].clearMessage()&lt;BR /&gt;&amp;nbsp; &amp;nbsp; return&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;params[1] is an output featureclass.&amp;nbsp; params[2] is an input featureclass.&amp;nbsp; Both are optional.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 29 Nov 2019 17:38:31 GMT</pubDate>
    <dc:creator>williamwinner</dc:creator>
    <dc:date>2019-11-29T17:38:31Z</dc:date>
    <item>
      <title>arcpy make Parameter to be both input/output</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-make-parameter-to-be-both-input-output/m-p/418879#M32889</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Is it possible to have a arcpy's Parameter which act as both input and output?&amp;nbsp; Specifically,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;The parameter is for a file (CSV file)&lt;/LI&gt;&lt;LI&gt;If specified file does not exist, create one and use it as output (this is how my script is written)&lt;/LI&gt;&lt;LI&gt;If specified file exists, read it, do some consistency check with other inputs, and then append to the file&lt;/LI&gt;&lt;LI&gt;If specified file exists but doenst make sense, just delete and start over&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I thought about different way to make this happen, but issues I have are&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;if parameter is output, then the file got deleted before execute() method got called.&amp;nbsp; so I cannot use output&lt;/LI&gt;&lt;LI&gt;if I make parameter to input, the toolbox's dialog doesn't let me specified non-existent name, forces me to pick existing file name.&amp;nbsp; that's not what I want, because the file is actually output&lt;/LI&gt;&lt;LI&gt;so I may set parameter as output, and do the testing in updateMessage() method, and if I want to append, I copy the parameter value to a hidden input parameter and then delete value from the output parameter?&amp;nbsp; is this feasible?&lt;/LI&gt;&lt;LI&gt;I can have two boxes, one for input, other for output, and pick the one that make sense.&amp;nbsp; but. the UI is going to be very confusing.&lt;/LI&gt;&lt;LI&gt;one other way is to not set this parameter to string was something.&amp;nbsp; but then the file browser like behavior of the UI doesn't work (is there way to use the browser like interface for any variable?&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If there is some simple method to do this, please let me know.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 11 Mar 2015 23:50:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-make-parameter-to-be-both-input-output/m-p/418879#M32889</guid>
      <dc:creator>yosukekimura</dc:creator>
      <dc:date>2015-03-11T23:50:16Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy make Parameter to be both input/output</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-make-parameter-to-be-both-input-output/m-p/418880#M32890</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;can you post your script for us to review?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 12 Mar 2015 01:13:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-make-parameter-to-be-both-input-output/m-p/418880#M32890</guid>
      <dc:creator>benberman</dc:creator>
      <dc:date>2015-03-12T01:13:13Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy make Parameter to be both input/output</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-make-parameter-to-be-both-input-output/m-p/418881#M32891</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have the Tool as class, but it's rather large.&amp;nbsp; I can make stripped down version if needed.&amp;nbsp; But really, I haven't done much beyond very basic things.&amp;nbsp; I just asked before spending hours for reinventing wheels&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This is what I have for the parameter right now&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def getParameterInfo(self):
&amp;nbsp;&amp;nbsp;&amp;nbsp; # other params ...

&amp;nbsp;&amp;nbsp;&amp;nbsp; ## Output parameter

&amp;nbsp;&amp;nbsp;&amp;nbsp; out_csv = arcpy.Parameter(
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; displayName="Output CSV File (.csv)",
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; name="out_csv",
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; datatype="DEFile",
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; parameterType="Required",
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; direction="Output")
&amp;nbsp;&amp;nbsp;&amp;nbsp; out_csv.filter.list = ['csv']
&amp;nbsp;&amp;nbsp;&amp;nbsp; # more...&lt;/PRE&gt;&lt;P&gt;In the updateMessage(), I have this piece of code, where I may write checking code instead of write(f.next()) .&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def updateMessage(self, parameters):
&amp;nbsp;&amp;nbsp;&amp;nbsp; # other things...

&amp;nbsp;&amp;nbsp;&amp;nbsp; # my test
&amp;nbsp;&amp;nbsp;&amp;nbsp; canappend = False
&amp;nbsp;&amp;nbsp;&amp;nbsp; pth = out_csv.valueAsText
&amp;nbsp;&amp;nbsp;&amp;nbsp; if pth is not None and os.path.exists(pth):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # so this does lets me read the file
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # so suppose i write some small code to do the check, and call that when needed
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; with open(pth,'r') as f:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # TODO this part should be some testing code
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fo.write(f.next())
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; canappend = True
&amp;nbsp;&amp;nbsp;&amp;nbsp; # more tests ...&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 18:57:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-make-parameter-to-be-both-input-output/m-p/418881#M32891</guid>
      <dc:creator>yosukekimura</dc:creator>
      <dc:date>2021-12-11T18:57:09Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy make Parameter to be both input/output</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-make-parameter-to-be-both-input-output/m-p/418882#M32892</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Here is more or less what I would do...&amp;nbsp; I am taking method 4 of my original post.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Tool has two boxes, one grayed out (Figure 1).&amp;nbsp; Usually specify output file name in top box (Figure 2).&amp;nbsp; If I put a file which has partial but valid data, script moves the file name to bottom clear the top box (Figure 3).&amp;nbsp; execute() find value for the second box (Parameter in_csv ) instead of the first box (out_csv), and append the data.&amp;nbsp; If the file provided is not compatible with the tool, tool is going to overwrite the file (Figure 4).&amp;nbsp; Code at the end.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Figure 1. Intial dialog&lt;/P&gt;&lt;P&gt;&lt;IMG alt="append.png" class="image-1 jive-image" src="https://community.esri.com/legacyfs/online/72813_append.png" style="width: 620px; height: 478px;" /&gt;&lt;/P&gt;&lt;P&gt;Figure 2. New file&lt;/P&gt;&lt;P&gt;&lt;IMG alt="append_new.png" class="image-2 jive-image" src="https://community.esri.com/legacyfs/online/72814_append_new.png" style="width: 620px; height: 478px;" /&gt;&lt;/P&gt;&lt;P&gt;Figure 3. Appending to partial file. I set the file name in first box, and the script judge that it is for appending and move it the second box&lt;/P&gt;&lt;P&gt;&lt;IMG alt="append_append.png" class="image-3 jive-image" src="https://community.esri.com/legacyfs/online/72917_append_append.png" style="width: 620px; height: 478px;" /&gt;&lt;/P&gt;&lt;P&gt;Figure 4. Overwriting existing file.&amp;nbsp; In this case the csv file I provided is not what's expected for appending, so tool is going to overwrite.&lt;/P&gt;&lt;P&gt;&lt;IMG alt="append_overwrite.png" class="image-4 jive-image" src="https://community.esri.com/legacyfs/online/72918_append_overwrite.png" style="width: 620px; height: 478px;" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The code:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;(too long?)&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 12 Mar 2015 19:25:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-make-parameter-to-be-both-input-output/m-p/418882#M32892</guid>
      <dc:creator>yosukekimura</dc:creator>
      <dc:date>2015-03-12T19:25:53Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy make Parameter to be both input/output</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-make-parameter-to-be-both-input-output/m-p/418883#M32893</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;That's a bit of an inspired solution.&amp;nbsp; It's silly that ESRI doesn't allow you to append to an output and you can't select an input that doesn't exist.&amp;nbsp; But doing this allows it to work well.&amp;nbsp; I just wanted to add to this with some code.&amp;nbsp; Here's how I implemented that Validation code:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;def __init__(self):&lt;BR /&gt;&amp;nbsp; &amp;nbsp; """Setup arcpy and the list of tool parameters."""&lt;BR /&gt;&amp;nbsp; &amp;nbsp; self.params = arcpy.GetParameterInfo()&lt;BR /&gt;&amp;nbsp; &amp;nbsp; self.params[2].enabled = False&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;def updateParameters(self):&lt;BR /&gt;&amp;nbsp; &amp;nbsp; """Modify the values and properties of parameters before internal validation is performed. This method is called whenever a parameter has been changed."""&lt;BR /&gt;&amp;nbsp; &amp;nbsp; if self.params[1].altered:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if not self.params[1].value is None:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if arcpy.Exists(self.params[1].value):&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; self.params[2].value = self.params[1].valueAsText&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; self.params[1].value = None&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; else:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; self.params[2].value = None&lt;BR /&gt;&amp;nbsp; &amp;nbsp; return&lt;/P&gt;&lt;P&gt;def updateMessages(self):&lt;BR /&gt;&amp;nbsp; &amp;nbsp; """Modify the messages created by internal validation for each tool parameter. This method is called after internal validation."""&lt;BR /&gt;&amp;nbsp; &amp;nbsp; if self.params[1].value is None and self.params[2].value is None:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; self.params[1].setErrorMessage("Must enter a value")&lt;BR /&gt;&amp;nbsp; &amp;nbsp; else:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; self.params[1].clearMessage()&lt;BR /&gt;&amp;nbsp; &amp;nbsp; return&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;params[1] is an output featureclass.&amp;nbsp; params[2] is an input featureclass.&amp;nbsp; Both are optional.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 29 Nov 2019 17:38:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-make-parameter-to-be-both-input-output/m-p/418883#M32893</guid>
      <dc:creator>williamwinner</dc:creator>
      <dc:date>2019-11-29T17:38:31Z</dc:date>
    </item>
  </channel>
</rss>

