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 = "2022",
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", November, 2022. [Online]. Available: https://www.gkbrk.com/qna/python-read-lines-stdin/. [Accessed Nov. 24, 2022].
Yaltirakli, G. (2022, November 24). 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 (Nov. 24, 2022), https://www.gkbrk.com/qna/python-read-lines-stdin/