【发布时间】:2016-10-13 20:02:20
【问题描述】:
我尝试将脚本的输出重定向到一个文件。 我不想做类似的事情
python myscript.py > xy.out
因为很多变量都存储在我的 ipython 环境中,我喜欢将其保留。
我尝试点击这个链接
IPython: redirecting output of a Python script to a file (like bash >)
但是,当我尝试这样做时
with redirect_output("my_output.txt"):
%run my_script.py
它给出了错误
---> 10 self.sys_stdout = sys.stdout
NameError: global name 'sys' is not defined
有一个类似的解决方案可以将 ipython shell 的输出复制到文件中,但它说我的单元格没有定义
https://www.quora.com/How-do-I-export-the-output-of-the-IPython-command-to-a-text-file-or-a-CSV-file
有没有更简单的方法或 ipython 内置功能可以做到这一点? 例如
在 oracle sqlplus 中有一个 spool 命令 例如
spool /tmp/abc
select * from table_x;
spool off
现在sql语句输出在/tmp/abc
ipython 有这样的等价物吗??
【问题讨论】:
-
在尝试
redirect_output功能之前,您是否import sys?