IPython: redirecting output of a Python script to a file (like bash >)
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: 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 How do I do this when I run the script in IPython, i.e. like execfile('my_script.py') There is an older page describing a function that could be written to do this, but I believe that there is now a built-in way to do this that I just can't find. 回答1: IPython has its own context manager for capturing stdout/err , but it doesn't redirect to files, it redirects to an object: from