Home > Article > Backend Development > How to read content from standard input stdin in PYTHON
Python has several ways to read data from standard input.
1. sys.stdin
sys.stdin provides read() and readline() functions. If you want to read line by line, you can Consider using it:
import sys line = sys.stdin.readline() while line: print line, line = sys.stdin.readline()
Note: If there is no data, io will be blocked, so you can do data checking on the standard input (Linux):
import sys import select if select.select([sys.stdin], [], [], 0.0)[0]: help_file_fragment = sys.stdin.read() else: print("No data", file=sys.stderr) sys.exit(2)
2, input()
If you want to use the interactive method (prompt for input), Python 3.x can use input(), while Python 2.x uses raw_input():
x = raw_input('请输入您的名字:')
The above is the detailed content of How to read content from standard input stdin in PYTHON. For more information, please follow other related articles on the PHP Chinese website!