

Python Use StringIO to Capture STDOUT and STDERR
source link: https://www.devdungeon.com/content/python-use-stringio-capture-stdout-and-stderr
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Introduction
When you use print()
in python the output goes to standard output or sys.stdout
.
You can directly call sys.stdout.write()
instead of using print()
, but you
can also completely replace sys.stdout
with another stream.
This example will show you how to use StringIO
memory files to capture
and view stdout.
As always, check the official StringIO documentation for the best information.
Use StringIO to replace stdout
This is good for testing and special cases where you may not have a terminal output.
First, create the StringIO
object and then replace sys.stdout
with it.
You can write to it directly, or when using print()
it will default to going
to sys.stdout
and therefore go to our stream object.
To get back the original output stream, it is stored from sys.__stdout__
a special
dunder that always contains the original system stdout.
from io import StringIO # Python 3
import sys
# Create the in-memory "file"
temp_out = StringIO()
# Replace default stdout (terminal) with our stream
sys.stdout = temp_out
print("This is going in to the memory stream")
temp_out.write("Can be written to like normal.\n")
# The original `sys.stdout` is kept in a special
# dunder named `sys.__stdout__`. So you can restore
# the original output stream to the terminal.
sys.stdout = sys.__stdout__
print("Now printing back to terminal")
print("The 'fake' stdout contains: =======")
sys.stdout.write(temp_out.getvalue())
print("=============================")
Note that the StringIO
object is the same type of object created when you
call open()
and open a file with the r
flag.
There is also an io.BytesIO
object for byte streams, which is the type
of object you get when calling open()
with rb
.
Also note that there is sys.stderr
and sys.stdin
that you can
manipulate in the same ways. They also have the corresponding
sys.__stderr__
and sys.__stdin__
originals.
Conclusion
After reading this, you should know how to create in-memory files for both text and binary data as well as how to temporary replace the default system standard out with your own object to capture data.
References
Recommend
-
15
Python UTF-8 print fails when redirecting stdoutPython UTF-8 print fails when redirecting stdout Wed 26 January 2011Consider the following piece of code: # -*- coding: utf-8 -*- print u"Վարդանաշեն" Runni...
-
30
Introduction Among the popular operating systems, they have all standardized on using standard input, standard output, and standard error with file desciptors 0, 1, and 2 respectively. This allows you to pipe the inputs and outputs...
-
28
Introduction Operating systems recognize a couple special file descriptor IDs: STDIN - 0 - Input usally coming in from keyboard. STDOUT - 1 - Output from the...
-
17
Bash打印中的stdout与stderr 2017-07-26 10:58:36 +08 字数:2134 标签: Bash 简介stdout与stderr
-
21
在 Python 里设置 stdout 的编码 本文来自依云's Blog,转载请注明。
-
10
将命令的输出重定向到文件或将其通过管道传递到另一个命令时,您可能会注意到错误消息已打印在屏幕上。在Bash和其他Linux Shell中,执行程序时,它使用三个标准I/O流。 每个流由一个数字文件描述符表示:0-stdin...
-
12
命令列工具的 stdout, stderr 輸出與 .NET 整合應用-黑暗執行緒 昨天提到的 Linux 掃描工具 - scanimage,剛好有個經典輸出分流行為,scanimage 將圖檔傳到標準輸出(Standard Output),...
-
10
nohup: redirecting stderr to stdout解决办法
-
11
Python标准库: StringIO模块——内存文件 May 30, 2018 工具,
-
9
Conversation Contributor In all other places (e.g. bootstrap.py, opt-dis...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK