17

I have a large Mathematica notebook that uses Print[] commands periodically to output runtime messages. This is the only output (aside from exported files) that this notebook generates. Is there any way I can automate the export of this output to a .txt file without having to re-write the Print[] commands?

Nakilon
  • 34,866
  • 14
  • 107
  • 142
paradox09
  • 171
  • 2
  • 4

2 Answers2

18

According to the documentation, Print outputs to the $Output channel which is a list of streams. So, at the beginning of the notebook,

strm = OpenWrite["output.log"];
AppendTo[ $Output, strm ];

and at the end of the notebook

Close[strm];

Note, if execution is interrupted prior to closing the stream, then you'll have to do it manually. Also, the above code will overwrite prior data in "output.log," so you may wish to use OpenAppend, instead.

Edit: to guarantee that Abort will be called, consider using one of the techniques outlined here.

Community
  • 1
  • 1
rcollyer
  • 10,475
  • 4
  • 48
  • 75
  • Nice! I posted a more complex one with `Print` redefinitions, but this one is clearly better. It pays off to read the docs sometimes:) +1 – Leonid Shifrin Oct 12 '11 at 14:29
  • @LeonidShifrin, I saw yours, and was going to comment on winning the simplicity game. But, you beat me to it by deleting it. :( – rcollyer Oct 12 '11 at 14:30
  • 2
    @LeonidShifrin You shouldn't delete the answer. Having an alternate solution is always good, as it shows how the same task can be achieved in different ways in Mathematica. Also, if the OP indeed did want to redefine `Print` for something else, your answer would serve as a template. – abcd Oct 12 '11 at 14:35
  • @yoda I am sure Leonid used [the Villegas-Gayley trick](http://stackoverflow.com/questions/4198961/what-is-in-your-mathematica-tool-bag/5149656#5149656) for the `Print` redefinition. It is quite common and you can find the template at the link above. – Alexey Popkov Oct 12 '11 at 14:45
  • 3
    @Leonid, I am shocked you didn't know this. I guess you're not a consortium of Mathematica experts after all. ;-) – Mr.Wizard Oct 12 '11 at 14:51
  • @yoda Alexey is correct, I used that trick. So, I guess, no need to worry, it was used in many places here on SO already. – Leonid Shifrin Oct 12 '11 at 14:56
  • 1
    @Mr.Wizard Then we are even finally, since I had a similar shock regarding you, some time ago. For a long while, I was sure that it was you who was a consortium of some sort, and I think I was not the only one thinking this ;) – Leonid Shifrin Oct 12 '11 at 14:58
  • 1
    @LeonidShifrin and Mr.Wizard, I think both of you must be consortia. It is only that they are not as complete as the chief persona let's on. – rcollyer Oct 12 '11 at 15:33
2

You want the PutAppend command.

cah
  • 574
  • 3
  • 4
  • 1
    That works, but doesn't meet the OPs requirement for not having to change all of his `Print` statements. See my [solution](http://stackoverflow.com/questions/7741432/exporting-mathematica-print-output-to-a-txt-file/7741627#7741627). – rcollyer Oct 12 '11 at 14:26