IPython:将 Python 脚本的输出重定向到文件(如 bash >)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14571090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:51:15  来源:igfitidea点击:

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

pythonioipython

提问by wwwilliam

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

我有一个要在 IPython 中运行的 Python 脚本。我想将输出重定向(写入)到一个文件,类似于:

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')

当我在 IPython 中运行脚本时,我该怎么做,比如 execfile('my_script.py')

There is an older pagedescribing 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.

有一个较旧的页面描述了可以编写来执行此操作的函数,但我相信现在有一种内置的方法可以执行此操作,但我找不到。

采纳答案by minrk

IPython has its own context manager for capturing stdout/err, but it doesn't redirect to files, it redirects to an object:

IPython 有自己的上下文管理器用于捕获 stdout/err,但它不会重定向到文件,而是重定向到一个对象:

from IPython.utils import io
with io.capture_output() as captured:
    %run my_script.py

print captured.stdout # prints stdout from your script

And this functionality is exposed in a %%capturecell-magic, as illustrated in the Cell Magics example notebook.

并且此功能在%%capturecell-magic 中公开,如Cell Magics 示例笔记本 中所示

It's a simple context manager, so you can write your own version that would redirect to files:

这是一个简单的上下文管理器,因此您可以编写自己的版本来重定向到文件:

class redirect_output(object):
    """context manager for reditrecting stdout/err to files"""


    def __init__(self, stdout='', stderr=''):
        self.stdout = stdout
        self.stderr = stderr

    def __enter__(self):
        self.sys_stdout = sys.stdout
        self.sys_stderr = sys.stderr

        if self.stdout:
            sys.stdout = open(self.stdout, 'w')
        if self.stderr:
            if self.stderr == self.stdout:
                sys.stderr = sys.stdout
            else:
                sys.stderr = open(self.stderr, 'w')

    def __exit__(self, exc_type, exc_value, traceback):
        sys.stdout = self.sys_stdout
        sys.stderr = self.sys_stderr

which you would invoke with:

你会用它来调用:

with redirect_output("my_output.txt"):
    %run my_script.py

回答by Silas Ray

There's the hacky way of overwriting sys.stdoutand sys.stderrwith 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 printstatements.

有一种覆盖sys.stdoutsys.stderr文件对象的hacky方式,但这确实不是一个好方法。真的,如果你想控制 python 内部输出的去向,你需要实现某种日志记录和/或输出处理系统,你可以通过命令行或函数参数而不是使用print语句来配置它。

回答by Mookayama

It seems a lot of code.... My solution. redirect output of ipython script into a csv or text file like sqlplus spoolwonder there is an easy way like oracle sqlplus spool command..?

似乎很多代码....我的解决方案。 将 ipython 脚本的输出重定向到 csv 或文本文件,如 sqlplus spool想知道有没有像 oracle sqlplus spool 命令这样的简单方法..?

回答by eiffel

Writing a script seemed overkill for me, as I just wanted something simple to look at a lot of text contained in a variable while working in IPython. This is what worked for me:

编写脚本对我来说似乎有点过分了,因为我只是想要一些简单的东西来在 IPython 中工作时查看变量中包含的大量文本。这对我有用:

%store VARIABLE >> file.txt(appends)
%store VARIABLE > file.txt(overwrites)

%store VARIABLE >> file.txt(附加)
%store VARIABLE > file.txt(覆盖)

回答by Petteri Nevavuori

While this an old question, I found this and the answers as I was facing a similar problem.

虽然这是一个老问题,但当我面临类似问题时,我发现了这个问题和答案。

The solution I found after sifting through IPython Cell magics documentationis actually fairly simple. At the most basic the solution is to assign the output of the command to a variable.

我在筛选IPython Cell magics 文档后找到的解决方案实际上相当简单。最基本的解决方案是将命令的输出分配给一个变量。

This simple two-cell example shows how to do that. In the first Notebook cell we define the Python script with some output to stdoutmaking use of the %%writefilecell magic.

这个简单的两格示例展示了如何做到这一点。在第一个 Notebook 单元格中,我们定义了 Python 脚本和一些输出以stdout利用%%writefile单元格魔法。

%%writefile output.py
print("This is the output that is supposed to go to a file")

Then we run that script like it was run from a shell using the !operator.

然后我们运行该脚本,就像使用!操作符从 shell 运行一样。

output = !python output.py
print(output)
>>> ['This is the output that is supposed to go to a file']

Then you can easily make use of the %storemagic to persist the output.

然后你可以轻松地利用%store魔法来持久化输出。

%store output > output.log

Notice however that the output of the command is persisted as a list of lines. You might want to call "\n".join(output)prior storing the output.

但是请注意,命令的输出作为行列表持久化。您可能希望"\n".join(output)在存储输出之前调用。

回答by Raful Chizkiyahu

use this code to save the output to file

使用此代码将输出保存到文件

import time
from threading import Thread
import sys
#write the stdout to file
def log():
    #for stop the thread
    global run
    while (run):
        try:
            global out
            text = str(sys.stdout.getvalue())
            with open("out.txt", 'w') as f:
                f.write(text)
        finally:
            time.sleep(1)
%%capture out
run = True
print("start")
process = Thread(target=log, args=[]).start()

# do some work
for i in range(10, 1000):
    print(i)
    time.sleep(1)
run= False
process.join()

It is useful to use a text editor that tracer changes the file and suggest reloading the file like notepad++

使用跟踪器更改文件并建议重新加载文件(如记事本++)的文本编辑器很有用