使用自定义类捕获 Python 控制台输出 - Capturing Console Output in Python Using Custom Class

在python中,当执行一段代码时,如果想要获取这段代码输出到控制台的信息的话,可以使用重定向stdout的方法。

In Python, if you want to capture the information output to the console when executing a piece of code, you can use the method of redirecting stdout.


首先,建立一个类,里面有两个函数,write和flush

First, establish a class with two functions, write and flush.

1
2
3
4
5
6
7
8
9
class StdoutRedirect:
def __init__(self):
self.content = ''

def write(self, text):
self.content += text

def flush(self):
self.content = ''


实例化这个类,并且重定向sys.stdout

Instantiate this class and redirect sys.stdout to it.

1
2
my_stdout = StdoutRedirect()
sys.stdout = my_stdout

之后的任何输出都会进入my_stdout的content里。

Any subsequent output will go into the content of my_stdout.


在所需重定向输出的代码结束后,只需将sys.stdout重定向回默认的输出流即可。

At the end of the code where you need to redirect the output, you just need to redirect sys.stdout back to the default output stream.

1
sys.stdout = sys.__stdout__


完整代码如下

The complete code is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import sys


class StdoutRedirect:
def __init__(self):
self.content = ''

def write(self, text):
self.content += text

def flush(self):
self.content = ''


my_stdout = StdoutRedirect()
sys.stdout = my_stdout

print(114514)

sys.stdout = sys.__stdout__
result = my_stdout.content
my_stdout.flush()

print("result: " + result)