Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to redirect stderr to file in Jupyter?

There was a redirect_output function in IPython.utils, and there was a %%capture magic function, but these are now gone, and this thread on the topic is now outdated.

I'd like to do something like the following:

from IPython.utils import io
from __future__ import print_function
with io.redirect_output(stdout=False, stderr="stderr_test.txt"):
    while True:
        print('hello!', file=sys.stderr)

Thoughts? For more context, I am trying to capture the output of some ML functions that run for hours or days, and output a line every 5-10 seconds to stderr. I then want to take the output, munge it, and plot the data.

like image 244
paulperry Avatar asked Oct 27 '25 10:10

paulperry


1 Answers

You could probably try replacing sys.stderr with some other file descriptor the same way as suggested here.

import sys
oldstderr = sys.stderr
sys.stderr = open('log.txt', 'w')
# do something
sys.stderr = oldstderr

Update: starting form Python 3.4, you should consuder using contextlib.redirect_stdout() instead, like this:

f = io.StringIO()
with redirect_stdout(f):
    print('a')
s = f.getvalue()
like image 90
Ben Usman Avatar answered Oct 30 '25 00:10

Ben Usman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!