I have the following simple Python code.
stdout = sys.stdout
stderr = sys.stderr
try:
# Omitted
finally:
sys.stdout = stdout
After searching, I found that sys.stdin, sys.stdout, and sys.stderr are file objects corresponding to the interpreter's standard input, standard output and standard error streams. So, my guess here is that we first assign the sys.stdout and sys.stderr to the variables stdout and stderr.
Then, even if there are exceptions in the try statement, can we always get the sys.stdout back? Is this correct? I felt that I was still confused.
Synopsis. stdin , stdout , and stderr are predefined file objects that correspond to Python's standard input, output, and error streams. You can rebind stdout and stderr to file-like objects (objects that supply a write method accepting a string argument) to redirect the destination of output and error messages.
Python stderr is known as a standard error stream. It is similar to stdout because it also directly prints to the console but the main difference is that it only prints error messages. Example: import sys sys.stderr.write("This is error msg")
stdout. A built-in file object that is analogous to the interpreter's standard output stream in Python. stdout is used to display output directly to the screen console. Output can be of any form, it can be output from a print statement, an expression statement, and even a prompt direct for input.
stdout − It stands for standard output, and is used to text output of any command you type in the terminal, and then that output is stored in the stdout stream. stderr − It stands for standard error. It is invoked whenever a command faces an error, then that error message gets stored in this data stream.
In normal C programs, there are three "files" open by default: stdin, stdout, and stderr. When you do input and output in C, by default they come from stdin and stdout. But, you can also use them where code expects files, or reassign them to be new files.
Python seeks to "mimic" this behavior of C. When you print()
in Python, your text is written to Python's sys.stdout
. When you do input()
, it comes from sys.stdin
. Exceptions are written to sys.stderr
.
You can reassign these variables in order to redirect the output of your code to a file other than stdout. This is very similar to shell redirection, if you're familiar with that. The reason you might do something like this is to keep a log of your program's output or make code "shut up", i.e. not send output to stdout. So, in your example example:
stdout = sys.stdout
try:
sys.stdout = open('file.txt', 'w')
print('blah')
# etc
finally:
sys.stdout.close() # close file.txt
sys.stdout = stdout
sys.stderr = stderr
This code wouldn't print anything to the console, but it would write "blah" to a text file named file.txt
. To make this sort of thing less error-prone, Python provides sys.__stdin__
and sys.__stdout__
, which always hold the original values of sys.stdin
and sys.stdout
. The above code could be made simpler using this:
try:
sys.stdout = open('file.txt', 'w')
print('blah')
# etc
finally:
sys.stdout.close() # close file.txt
sys.stdout = sys.__stdout__
The reason Python has both stdout
and __stdout__
is mainly for convenience, so you don't have to make a variable to back up stdout
.
However, I have to recommend that you don't make a habit out of reassigning stdout
. It's pretty easy to mess things up that way! If you want to save your code's output, you can use shell redirection. If you want to have the ability to keep a log of what your program is doing, see Python's logging module. If you want your code to be quieter, give your functions a quiet=False
parameter so you can shut them up when you want! There is rarely a "genuine" need to do reassign stdout
, etc. Python allows you to do it, because Python gives programmers a lot of control, and expects them to be responsible. You could do something like this, if you really wanted to:
>>> import random
>>> random.random = lambda: 1
>>> random.random()
1
>>> random.random()
1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With