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?

  • When you say, "it automatically erases the file" do you mean the .csv file that you are appending to? – djq Jan 24 '11 at 23:02
  • yes, the CSV output file is being deleted by ArcGIS before I have a chance to test whether or not it exists (and make a decision to create new or append). I have the "if os.path.exists(file)" as the second line of code following the import statements. – RyanKDalton Jan 24 '11 at 23:19

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

  • I do think that the logic of deleting existing files should be left up to the script, but thanks for getting to the bottom of this! – blah238 Jan 26 '11 at 17:13
  • Generally I would agree but you need to think of this not in a python context, but an ArcGIS context. If you envision the CSV as a SHP, and you are telling ArcGIS that you want to overwrite the shp with the output of your operation, you wouldn't always want to have to add in a "Delete" tool/statement. You just expect the tool to automatically overwrite the pre-existing file. That is what is actually happening here. – RyanKDalton Jan 27 '11 at 16:28

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.

  • 1
    Unfortunately I have to agree. I stripped out all code from my script and simply made the output file reference a blank script (no text in it at all). It still deleted the output file, which definitely sounds like a bug to me. Even though the suggested route is not as elegant as I would like, I will give it a try and if it works, just be happy I have a solution. – RyanKDalton Jan 25 '11 at 18:35

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?

  • Great idea, but based upon my test where I grabbed the report file variable right away and did a file exists (before any arcgis operations were used), it appears ArcGIS is deleting the old file as soon as I hit the OK button on the dialog. – RyanKDalton Jan 24 '11 at 21:48

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.