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?


2 Answers 2


This should overwrite :

 k > (OutputFile);

While this appends :

 k >> (OutputFile);
  • $\begingroup$ Thank you, this helped $\endgroup$
    – odd
    Commented Oct 12, 2020 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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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