# Overwrite on a txt file from AMPL

I have a result I want to save in a .txt file. So I use this line to save a result k:

param OutputFile symbolic := "output.txt";
print k >> (OutputFile);


However, I may run the script several times which means I will open an existing file and add lines to it. What I want to do is overwrite on the file output.txt so I will not have the previous runs results.

How do I do this?

This should overwrite :

 k > (OutputFile);


While this appends :

 k >> (OutputFile);

• Thank you, this helped – odd Oct 12 '20 at 14:16

To overwrite a file that you have previously been writing in an AMPL session, first close the file:

close (OutputFile);


Then use > to overwrite the file:

print k > (OutputFile);


This deletes the file and then opens a new, empty file with the same name. Note that once the file is open, > and >> both append to it. A complete description of AMPL file redirection is given in section 12.7 General facilities for manipulating output of the AMPL book.