

Using stdin, stdout, and stderr in Python
source link: https://www.devdungeon.com/content/using-stdin-stdout-and-stderr-python
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
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 to different locations. Let's look at how to utilize standard input, output, and error in Python.
To learn more about piping, redirection, stdin, stdout, and stderr in general, see my tutorial STDIN, STDOUT, STDERR, Piping, and Redirecting.
Basic usage
In Python, the sys.stdin
, sys.stdout
, and sys.stderr
are file-like objects that can perform
expected operations like read()
and write()
. Let's look at how to use these objects.
Refer to the official sys package documentation for full information.
Standard output
Standard output
print(x)
is basically a shortcut for sys.stdout.write(x + '\n')
import sys
# Standard output - sys.stdout
print(type(sys.stdout))
sys.stdout.write('Hello\n')
sys.stdout
is a io.TextIOWrapper
objects so you can read and write to them like a regular file.
See https://docs.python.org/3/library/io.html#io.TextIOWrapper for more details about the io.TextIOWrapper
class.
To pipe the output of your Python program to a file, you can do it from the shell like this:
python myapp.py > output.txt
Standard error
Standard error works just like standard output and can be used the same way. Standard error has file descriptor 2
where standard output has file descriptor 1
. This is beneficial if you want to separate warning and error messages from the actual output of your application. For example, if your program outputs an XML file, you don't want error strings injected in the middle of your XML file.
# Standard error - sys.stderr
print(type(sys.stderr))
sys.stderr.write("Error messages can go here\n")
To pipe standard error from the shell to a file while leaving standard output going to the terminal:
python myapp.py 2>errors.txt
To pipe standard error in to standard output, you can do:
python myapp.py 2>&1
Standard input
Standard input defaults to your keyboard in the terminal, but you can also pipe in files or the output from a previous program to your standard input. Here is a basic example of reading one byte from standard input:
# Standard input - sys.stdin
print(type(sys.stdin))
letter = sys.stdin.read(1) # Read 1 byte
print(letter)
# Can also do things like `sys.stdin.readlines()`
If you want interactive input from the user, it is better to use input()
instead of sys.stdin.read()
when asking for user input,
but sys.stdin.readlines()
can be useful for reading a file that was piped in from the shell like this:
# Feed `input_file.txt` to `sys.stdin` of the Python script
python my_script.py < input_file.txt
To pipe the standard output of one program to the standard input of your Python program, you can do it like this:
cat data.txt | python myapp.py
Dunder properties: sys.__stdin__, sys.__stdout__, sys.__stderr__
The dunder properties sys.__stdin__
, sys.__stdout__
and sys.__stderr__
always contain references
to the original streams. If you re-assign sys.stdout
to point somewhere else like a StringIO object,
you can always assign it back to the original value with the following.
Changing sys.stdout
to a StringIO
object can be useful especially when unit testing.
Check out my tutorial Python Use StringIO to Capture STDOUT and STDERR.
from io import StringIO
import sys
temp_output = StringIO()
# Replace stdout with the StringIO object
sys.stdout = temp_output
# Now, if you print() or use sys.stdout.write
# it goes to the string objc
print('This is going to the StringIO obecjt.')
sys.stdout.write('This is not going to the "real" stdout, yet')
# Then we can restore original stdout
sys.stdout = sys.__stdout__
print("Contents of the StringIO object")
print("===============================")
print(temp_output.getvalue())
fileinput.input() shortcut
This function will return standard input separated by line, or if file names
were provided as command-line arguments, it will provide all the lines from
the files provided. It is similar to ARGF
in Ruby. This gives you the option
to pipe in a file from the shell or to provide a list of file paths for input.
For example, you can either pipe in files via standard input or provide a list of filenames as arguments to the application:
python my_app.py < file1.txt
python my_app.py file1.txt file2.txt file3.txt
Here is an example of it in a script:
# fileinput_example.py
import fileinput
lines_of_data = fileinput.input()
print(type(lines_of_data)) # fileinput.FileInput
# One option: Join each line together to one long string
print(''.join(lines_of_data))
# Another option: Iterate through each line
# for line in lines_of_data:
# print(line.strip())
Here is how you can run the program to pipe in files or provide file names:
# Pipe file in via stdin
python fileinput_example.py < file1.txt
# Provide list of files as arguments
python fileinput_example.py file1.txt file2.txt file3.txt
Conclusion
After reading this guide, you should know how to access and read/write from standard input, standard output, and standard error in Python. You should also know how to use a StringIO
object to capture output, and use the fileinput.input()
function to get data.
References
Recommend
-
55
In this post I want to discuss faking (or redirecting ) standard input and output ( os.Stdin and os.Stdout ) in Go programs. This is often done in tests, but may also be useful in other scenarios. ...
-
21
2019-02-10IntroductionWhen I wrote my last two tools multicode and epoch, I wanted them to work with...
-
20
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 co...
-
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
-
10
将命令的输出重定向到文件或将其通过管道传递到另一个命令时,您可能会注意到错误消息已打印在屏幕上。在Bash和其他Linux Shell中,执行程序时,它使用三个标准I/O流。 每个流由一个数字文件描述符表示:0-stdin...
-
12
命令列工具的 stdout, stderr 輸出與 .NET 整合應用-黑暗執行緒 昨天提到的 Linux 掃描工具 - scanimage,剛好有個經典輸出分流行為,scanimage 將圖檔傳到標準輸出(Standard Output),...
-
10
nohup: redirecting stderr to stdout解决办法
-
17
In this Python tutorial, you will learn how do you read the input from stdin. Table Of Contents Let’s dive into the tutorial. Read input from stdin using sys.stdin
-
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