python sys.stdout

When we print things in the program, it is equivalent to stuffing things into the sys.stdout pipeline
PS: print = sys.stdout .write

So what is sys used for?

The sys module is used to manage the running environment of Python itself.Python is the interpreter and the program running on the operating system, so the sys package can be used to manage the parameters of Python running, such as memory, file size, etc.
Another important function is to interact with oneself

Here are a few sys package commands that we often use inadvertently
stdout/stderr/stdin

The stdin, stdout, and stderr variables contain stream objects corresponding to standard I/O streams. They are pipes built into every UNIX system
When we print print, we stuff the data to be printed in the pipe in stdout, stderr is the printing of error messages, the same as stdout

1. stdout is like a file-like object, because you can assign it to any file object and redirect the output

import sys
print(11111111)

__ console = sys.stdout   #For later restoration

# Print redirection file
f=open('outfile.log',"a+") 
sys.stdout=f
print('in outfile')

result

Previous print,Will print on the screen
11111111
Next print,Will be output to outfile.log,Will not print to the screen

If you want to restore it later, just change the pipeline to the original one.

sys.stdout = __console

When you print again at this time, it will be printed on the screen.

2. stdin standardized input, can be understood as input

import sys
name=sys.stdin.readline()print(name)

Here, when the Python interpreter executes to the second line of code, the system will stop there, waiting for the user to input data, and click Enter before executing the next line

print(name) is to print what you just entered

Recommended Posts

python sys.stdout
Python multithreading
Python CookBook
Python FAQ
Python3 dictionary
Python3 module
Python basics
Python descriptor
Python basics 2
Python exec
Python3 tuple
Python decorator
Python IO
Python multithreading
Python toolchain
Python3 list
Python multitasking-coroutine
Python overview
python introduction
Python analytic
Python basics
07. Python3 functions
Python basics 3
Python multitasking-threads
Python functions
python operator
Python entry-3
Centos 7.5 python3.6
Python string
python queue Queue
Python basics 4
Python basics 5
Centos6 install Python2.7.13
Python basic syntax (1)
Python exit loop
Ubuntu16 upgrade Python3
Centos7 install Python 3.6.
ubuntu18.04 install python2
Relearn ubuntu --python3
Python2.7 [Installation Tutorial]
Python 3.9 is here!
Python study notes (1)
python learning route
CentOS7 upgrade python3
Python3 basic syntax
Python review one
linux+ubuntu solve python
Functions in python
Python learning-variable types
CentOS install Python 3.6
7 features of Python3.9
Python file operation
ubuntu12.04 install python3
Python design patterns
Python - centos6 installation
Centos7 install Python2.7
01. Introduction to Python
100 small Python examples
Python network programming
python study notes
python_ file processing