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

本秂侑毒 提交于 2019-12-24 00:38:13

问题


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.


回答1:


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



回答2:


@Ben, just replacing sys.stderr did not work, and the full flush logic suggested in the post was necessary. But thank you for the pointer as it finally gave me a working version:

import sys
oldstderr = sys.stderr
sys.stderr = open('log.txt', 'w')
class flushfile():
    def __init__(self, f):
        self.f = f
    def __getattr__(self,name): 
        return object.__getattribute__(self.f, name)
    def write(self, x):
        self.f.write(x)
        self.f.flush()
    def flush(self):
        self.f.flush()
sys.sterr = flushfile(sys.stderr)
from __future__ import print_function
# some long running function here, e.g. 
for i in range(1000000):
    print('hello!', file=sys.stderr)
sys.stderr = oldstderr

It would have been nice if Jupyter kept the redirect_output() function and/or the %%capture magic.



来源:https://stackoverflow.com/questions/34145950/is-there-a-way-to-redirect-stderr-to-file-in-jupyter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!