[GIS] Append to text file using ArcGIS and Python

arcgis-9.3arcpypython-script-tool

I have wrote a small routine using Python for ArcGIS (9.3) that will get the count of 2 data layers, calc a percentage, and then write the text back to a text file (CSV) to create a sort of "report" that can be opened in excel and formatted. I want to be able to run this routine on many different dataset pairs and append the data to the end of my CSV report.

The problem is that I am having is that when I attach this Python script to an ArcGIS Script Tool, it automatically erases the file. I am testing that with

if os.path.exists(file):

right off the bat and this response is always "file does not exist". I have tried setting

gp.OverwriteOutput = 0

with no success, and am appending to the file using

fwrite = open(file, 'a')

Does anyone have a workaround that will allow me to choose an output file but NOT overwrite the output?

Best Answer

After some discussion with an Esri rep, I know have a solution and answer to WHY this issue is not really a bug (which I agree with). The reason this isn't really a bug is because Input is meant for files that already exist, Output is meant for new files, and I was trying to use the file selector as both either one. Ultimately this is just semantics but makes sense when you think about it.

Now the solution... In my dialog I created 3 input boxes: 1) A Required string type with Direction = Input and Value List = "New", "Append", 2) an Optional file type with Display name = "New", Direction = Output, and 3) an Optional file type with Display Name = "Existing", Direction = Input.

enter image description here

Now, in order to control which input box opens, thereby allowing my to choose either an output (new) or input (existing) file, I needed to put some code into the ToolValidator (Properties->Validation tab). Under the def initializeParameters(self) category I added the following code to disable the New & Existing file input boxes at startup:

self.params[1].Enabled = 0 # 0 = Disabled, 1 = Enabled on load
self.params[2].Enabled = 0 #Disable on load

and under the def updateParameters(self) category I added the following code to enable the correct file box based upon if I wanted to create a New or Append to existing file:

import sys, string, os
ftype = str(self.params[0].value)
if ftype == "New":
  self.params[1].Enabled = 1 #
elif ftype == "Append":
  self.params[2].Enabled = 1 

Then from within my scripting code, I can just identify which option was selected for the 1st parameter (New or Append), and grab the text string from the appropriate optional file string from parameter 2 or 3 with a simple If/Then statement.

More details on programming the ToolValidator here: http://webhelp.esri.com/arcgiSDEsktop/9.3/index.cfm?TopicName=Programming_a_ToolValidator_class

Related Question