Python read all lines from stdin Reading time: less than 1 minute
How to read all lines from standard input in Python? How can I read the standard input line by line?
In Python, the standard input is exposed as
sys.stdin is a file, the lines can be iterated just like any other file in Python.
for line in sys.stdin:
It is also possible to read the lines as
bytes instead of a Unicode
str. To do that, you can use the
.buffer property of the input stream.
for line in sys.stdin.buffer:
If you find this work useful, please cite it as:
title = "Python read all lines from stdin",
author = "Yaltirakli, Gokberk",
journal = "gkbrk.com",
year = "2023",
url = "https://www.gkbrk.com/qna/python-read-lines-stdin/"
Not using BibTeX? Click here for more citation styles.
Gokberk Yaltirakli, "Python read all lines from stdin", March, 2023. [Online]. Available: https://www.gkbrk.com/qna/python-read-lines-stdin/. [Accessed Mar. 27, 2023].
Yaltirakli, G. (2023, March 27). Python read all lines from stdin. https://www.gkbrk.com/qna/python-read-lines-stdin/
Gokberk Yaltirakli, Python read all lines from stdin, GKBRK.COM (Mar. 27, 2023), https://www.gkbrk.com/qna/python-read-lines-stdin/