[IPython-user] capture run output to a file

Fernando Perez fperez.net@gmail....
Tue Mar 27 00:32:02 CDT 2007

On 3/26/07, John Hunter <jdh2358@gmail.com> wrote:
> Is there a easyish, somewhat elegantish way to capture the output of a
> "run somefile.py" command to a file or file handle?

Not really, I'm afraid.

I sometimes use the code attached here for this purpose.  It's only a
minimally more sophisticated version of what you posted, and I use it
like this:

# Save all output to a file
if do_transcript:
    trans = utils.TranscriptPrint(start=True)

[... normal code runs ...]

# At the end, remember this:
if do_transcript:

You need to remember that if for any reason the script exits before
the final .stop() call is made, you should manually do it yourself
when you're back in ipython.  Otherwise you lose readline.  Obviously
a try/finally or a 2.5 context manager could be used to ensure the
stop() call is always made, but I don't actually bother with it.

It would be nice to have something like this available in a more
convenient fashion, a la 'tee' at the unix command line...


-------------- next part --------------
A non-text attachment was scrubbed...
Name: tprint.py
Type: text/x-python
Size: 2240 bytes
Desc: not available
Url : http://lists.ipython.scipy.org/pipermail/ipython-user/attachments/20070326/1af439f5/attachment.py 

More information about the IPython-user mailing list