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
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.
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