Home  >  Article  >  Backend Development  >  How to read content from standard input stdin in PYTHON

How to read content from standard input stdin in PYTHON

anonymity
anonymityOriginal
2019-05-25 10:35:446402browse

Python has several ways to read data from standard input.

How to read content from standard input stdin in PYTHON

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn