7
$\begingroup$

How to write output to an external text file in addition to the standard output stream, i.e. a notebook for a session with FE and stdout for command-line session?

$\endgroup$

1 Answer 1

7
$\begingroup$

I expected this to work but it apparently only writes Print output:

AppendTo[
  $Output, 
  OpenWrite["out.txt", FormatType -> OutputForm]
]

A workaround using $PrePrint:

output = OpenWrite["out.txt", FormatType -> OutputForm];

$PrePrint = (Write[output, #]; #) &;
$\endgroup$
2
  • $\begingroup$ Is it possible to use PutAppend[] and not OpenWrite[]? $\endgroup$
    – user13253
    Commented Jun 20, 2012 at 3:12
  • 1
    $\begingroup$ @MaThEmAtika how about OpenAppend? $\endgroup$
    – Mr.Wizard
    Commented Jun 20, 2012 at 3:47

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.