Python - stdin, stdout, and stderr

stdin , stdout , and stdderr variables contain stream objects corresponding to standard I/O streams.

If there is a need for better control of the output and the print does not meet our requirements, they are what we need.

They can also be replaced, in which case we can redirect the output and input to other devices, or process them in a non-standard way.

Related course: Complete Python Programming Course & Exercises


Test the standard output. You can write to standard console output by calling sys.stdout.write(). The characters \n add a new line.

#testing stdout  
>>> print('Hello World!') # This statement will print Hello World! on the standard output screen. 3.  
Hello World! 4.  
#Equivalent to.  
>>> import sys  
>>> sys.stdout.write('Hello World!\n')  
Hello World!


Let's test the standard input, sys.stdin. This is the information obtained from the standard input: keyboard, and then output to the standard output: screen.

#testing stdin  
>>> print('Hi,%s!' % input('Please enter your name:'))

So how does print and input in the above example relate to a standard input/output stream?

In fact, the standard input/output/error flow for Python programs is defined in the sys module as: sys.stdin, sys.stdout, sys.stderr

The standard input procedure for the above test is the same as the following. file.

#!usr/bin/env python  
import sys  
print('Please enter your name:'. 5. name=sys.stdin.readline()[:-1]  )
print('Hi,%s!' % name)

Operational results.

mallory@ubuntu:~$ python  
please enter your name:mallory

Hi, mallory!


So what exactly is sys.stdin, sys.stdout, stderr? We enter the following code in the Python runtime environment.

>>> import sys  
>>> sys.stdin  
<open file '<stdin>', mode 'r' at 0x7f63dd3cf0c0>  
>>> sys.stdout  
<open file '<stdout>', mode 'w' at 0x7f63dd3cf150>  
>>> sys.stderr  
<open file '<stderr>', mode 'w' at 0x7f63dd3cf1e0>

Redirection to a file

It can be seen that stdin, stdout, stderr are all objects of file properties in Python, They are automatically associated with standard inputs, outputs, and errors in the Shell environment when Python is started.

The I/O redirection of the Python program in the shell is exactly the same as the redirection of the DOS command,

This redirection is actually provided by Shell and has nothing to do with Python itself.

So can we redirect the stdin,stdout,stderr read and write operations to an internal object inside the Python program? The answer is yes.

Python provides a StringIO module to accomplish this idea, such as.

>>> from StringIO import StringIO  
>>> import sys  
>>> buf=StringIO()  
>> temp=sys.stdout #Save stdout before redirect  
>> sys.stdout=buf #redirects stdout to buff object  
>>>> print(825, 'python',0,666) # print will add a hard return to the information to be printed.  
>>> sys.stdout=temp #restorestdout  
>>>> buf.getvalue() # print adds a hard carriage return to the information to be printed, so it ends with a '\n'  
'825 python 0 666\n'