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

后端 未结 7 2098
慢半拍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:06

    For just one script to run I would do the redirection in bash

    ipython -c "execfile('my_script.py')" > my_output.txt
    

    On python 3, execfile does not exist any more, so use this instead

    ipython -c "exec(open('my_script.py').read())" > my_output.txt
    

    Be careful with the double vs single quotes.

提交回复
热议问题