IPython: redirecting output of a Python script to a file (like bash >)

后端 未结 7 2095
慢半拍i
慢半拍i 2020-12-01 03:50

I have a Python script that I want to run in IPython. I want to redirect (write) the output to a file, similar to:

python my_script.py > my_output.txt
         


        
7条回答
  •  一向
    一向 (楼主)
    2020-12-01 04:16

    There's the hacky way of overwriting sys.stdout and sys.stderr with a file object, but that's really not a good way to go about it. Really, if you want to control where the output goes from inside python, you need to implement some sort of logging and/or output handling system that you can configure via the command line or function arguments instead of using print statements.

提交回复
热议问题