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
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?
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.
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.Enabled = 0 # 0 = Disabled, 1 = Enabled on load
self.params.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:
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.
I can confirm that if the GP option to overwrite outputs is checked and a CSV or similar file is an output parameter, something in the unexposed GP tool validation code is deleting the output file before the script even begins to run. Sounds like a bug more than a feature but who knows.
Easiest solution would be to change the output parameter to a derived parameter, and use two input parameters to specify where to create the file and what to call it (one for the directory and another for the name of the file).
Then in your script set the value of the derived parameter (the full path to the output CSV file) using SetParameterAsText.
Can you read the contents of the file into memory (or another file) before running the process, and then merge the two files after the process?
Another approach would be to take a output folder and filenames as input. (You could also have an input checkbox (boolean) whether to overwrite or append.)
You could use tool validation to supply a default feedback and warn the user if the file exists already.