stdout redirect from Jupyter notebook is landing in the terminal

后端 未结 1 862
时光说笑
时光说笑 2020-12-18 01:17

So I was trying to redirect stdout for a some of the cells in my Jupyter Notebook to a file with this and then cancel it with this for the rest of the cells. Th

相关标签:
1条回答
  • 2020-12-18 02:02

    I'm not able to test that but the only explanation I see is that sys.stdout is not sys.__stdout__ but a rerouted/modified file object in order to be able to put data in cells (your comment indicates that it's a ipykernel.iostream.OutStream instance).

    So instead of resetting to sys.__stdout__, you should store the reference of sys.stdout:

    import sys
    old_stdout = sys.stdout
    sys.stdout = open('stdout.txt', 'w')
    ...
    sys.stdout = old_stdout
    

    Note that this method also works with a standard terminal.

    0 讨论(0)
提交回复
热议问题