将python/ipython交互式提示命令的输出重定向到文件或变量
如果在Python或Ipython命令提示符下执行函数,如“help(dir)”: 我希望在文件或变量中捕获结果输出,但是将python/ipython交互式提示命令的输出重定向到文件或变量,python,command-line,ipython,Python,Command Line,Ipython,如果在Python或Ipython命令提示符下执行函数,如“help(dir)”: 我希望在文件或变量中捕获结果输出,但是 >>> x = help(dir) >>> help(dir) >file.txt >>> help(dir) >>file.txt 不工作。我看到了一个相关的问题(),虽然它非常复杂,很难马上记住,甚至不清楚它是否适用于这里 在bashshell中,可以使用>或2>重定向输出。
>>> x = help(dir)
>>> help(dir) >file.txt
>>> help(dir) >>file.txt
不工作。我看到了一个相关的问题(),虽然它非常复杂,很难马上记住,甚至不清楚它是否适用于这里
在bashshell中,可以使用>或2>重定向输出。似乎在Python或Ipython shell中执行类似操作应该很容易。使用Ipython
capture\u output
函数
In [21]: from IPython.utils import io
In [22]: with io.capture_output() as captured:
....: help(dir)
....:
In [23]: print captured.stdout
Help on built-in function dir in module __builtin__:
dir(...)
dir([object]) -> list of strings
If called without an argument, return the names in the current scope.
更新
若上述解决方案不起作用,可以使用ipython命令输出捕获功能。例如:
In [6]: output = !python -c 'help(dir)' | cat
In [7]: output
Out[7]:
['Help on built-in function dir in module __builtin__:',
'',
'dir(...)',
' dir([object]) -> list of strings',
iPython magic%%capture比上述方法(两者都适用)更易于记忆,无需导入,甚至比上述方法更好:
In [38]: %%capture myout
....: help(dir)
....:
In [39]: print myout.stdout
Help on built-in function dir in module __builtin__:
dir(...)
dir([object]) -> list of strings
If called without an argument, return the names in the current scope.
Else, return an alphabetized list of names comprising (some of) the attributes
of the given object, and of attributes reachable from it.
If the object supplies a method named __dir__, it will be used; otherwise
the default dir() logic is used and returns:
for a module object: the module's attributes.
for a class object: its attributes, and recursively the attributes
of its bases.
for any other object: its attributes, its class's attributes, and
recursively the attributes of its class's base classes.
谢谢你,Dmitry-但是当我完全按照上面的方式打字时,我会得到不同的结果。在我相信你的评论被删掉之后。你得到不同的结果是什么意思?顺便说一句,我使用了iPythonShell。谢谢你,Dmitry-但是当我完全按照上面的方式输入时,我得到了不同的结果。我从IPython.utils导入io导入
。然后在[22]
中的之后,将显示帮助(dir)
的输出,而在[23]
中的之后,将不显示任何内容。我在python和ipython shell中得到了相同的结果。确实如此!非常感谢你,德米特里!我发现我可以用print'\n'.连接(输出)
-或保存到文件中,等等将结果更好地打印到屏幕上。这对我来说是最简单的解决方案。完成脚本后,只需使用open('filename.txt','a+')作为outfile:outfile.write(myout)
执行一个简单的,就完成了。
In [38]: %%capture myout
....: help(dir)
....:
In [39]: print myout.stdout
Help on built-in function dir in module __builtin__:
dir(...)
dir([object]) -> list of strings
If called without an argument, return the names in the current scope.
Else, return an alphabetized list of names comprising (some of) the attributes
of the given object, and of attributes reachable from it.
If the object supplies a method named __dir__, it will be used; otherwise
the default dir() logic is used and returns:
for a module object: the module's attributes.
for a class object: its attributes, and recursively the attributes
of its bases.
for any other object: its attributes, its class's attributes, and
recursively the attributes of its class's base classes.