Use the fileinput module:

import fileinput

for line in fileinput.input():
    pass

fileinput will loop through all the lines in the input specified as file names given in command-line arguments, or the standard input if no arguments are provided.

Note: line will contain a trailing newline; to remove it use line.rstrip().

🌐
DigitalOcean
digitalocean.com › community › tutorials › read-stdin-python
How to Read from stdin in Python | DigitalOcean
August 3, 2022 - Internally, it calls the input() function. The input string is appended with a newline character (\n) in the end. So, you can use the rstrip() function to remove it. Here is a simple program to read user messages from the standard input and process it. The program will terminate when the user ...
Discussions

Preferred method of reading from stdin?
The more pythonesque way is to use input() if you are accepting text from a human and use sys.stdin if you are reading a lot of redirected text, say from a pipe. More on reddit.com
🌐 r/learnpython
6
3
July 21, 2023
How to read line by line from stdin in python - Stack Overflow
Everyone knows how to count the ... that in python3, I find it is a puzzle. (counter.py) import sys chrCounter = 0 for line in sys.stdin.readline(): chrCounter += len(line) print(chrCounter) ... The answer is only the length of the first line "import sys". In fact, the program ONLY read the first line from the standard input, and discard the rest of them. It will be work if I take the place of sys.stdin.readline by ... More on stackoverflow.com
🌐 stackoverflow.com
[BEGINNERS] Intro to reading from standard input
This is very informative! Thank you so much! More on reddit.com
🌐 r/learnpython
1
18
January 6, 2018
How can you read utnil EOF from stdin, followed by a string?

The problem is that when you call cat data.txt | your_program, stdin is not connected to the terminal anymore - it's only fed with data.txt, and your input doesn't get there.

What you can do is instead, read /dev/tty. This file always represent the input/output of the actual terminal, not the stdio pipes.

Or, if you really want to read everything from stdin, then read_to_end is not the solution. You'll have to find your own "stop condition", like 2 newlines in a row (I think some old email clients used to do that).

More on reddit.com
🌐 r/rust
8
8
November 19, 2018
🌐
Better Stack
betterstack.com › community › questions › how-to-read-stdin-in-python
How do I read from stdin in Python? | Better Stack Community
October 5, 2023 - In Python, you can read from standard input (stdin) using the input() function. This function blocks execution and waits for the user to enter some text, which is then returned as a string.
🌐
Stack Abuse
stackabuse.com › reading-from-stdin-in-python
Reading from stdin in Python
August 28, 2023 - In Python, reading multiple lines from stdin can be accomplished in a couple of ways. One common approach is to use a for loop with sys.stdin. This allows you to iterate over each line that is entered until an EOF (End of File) character is received. ... In this code, each line that is entered ...
🌐
PhoenixNAP
phoenixnap.com › home › kb › devops and development › how to read from stdin in python
How to Read From stdin in Python | phoenixNAP KB
June 5, 2025 - The following code shows how to read files provided as command-line arguments: import fileinput for f in fileinput.input(): print(f) ... The code goes through the list of files and prints the file contents to the console.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › take-input-from-stdin-in-python
Take input from stdin in Python - GeeksforGeeks
July 12, 2025 - import sys for line in sys.stdin: if 'q' == line.rstrip(): break print(f'Input : {line}') print("Exit") ... The input() can be used to take input from the user while executing the program and also in the middle of the execution. ... # this accepts the user's input # and stores in inp inp = input("Type anything") # prints inp print(inp) ... If we want to read more than one file at a time, we use fileinput.input().
🌐
Sentry
sentry.io › sentry answers › python › read user input (stdin) in python
Read user input (STDIN) in Python | Sentry
November 15, 2023 - If we want to read multiple lines from stdin and don’t need to provide a prompt, we can use sys.stdin from Python’s built-in sys module. This allows us to treat stdin like a file.
🌐
PythonHow
pythonhow.com › how › read-from-stdin-standard-input
Here is how to read from stdin (standard input) in Python
import sys input_text = sys.stdin.read() print("The input was:", input_text)You can also use file object to read from stdin in python like this: ... *Please note that input() returns the user input as a string, so if you want to use the input as a number, you'll need to convert it using int() ...
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › how do you read from stdin in python?
How do you read from stdin in Python? - Spark By {Examples}
May 31, 2024 - In this article, we will explain how to read input from stdin in Python using the input() function and three other popular methods. These examples will give you a high-level idea of how to read input from the standard input stream in Python ...
🌐
Reddit
reddit.com › r/learnpython › [beginners] intro to reading from standard input
r/learnpython on Reddit: [BEGINNERS] Intro to reading from standard input
January 6, 2018 -

Hey everyone, I just want to talk about reading in data from standard input and the 4 main ways it can be done.

I'm not going to talk about the input() or raw_input() functions today, instead ill be talking about how to read from standard input using the sys module.

To get access to the sys module we first need to import it

import sys

Ok now we have access to this module, there are 3 ways to read from standard input:

  1. sys.stdin.read([size])

  2. sys.stdin.readline()

  3. sys.stdin.readlines()

Lets look at how all of these work first and the ways to use them.

First off we can read lines directly from the console, this will look something like this

lines = sys.stdin.read()
print(lines)

$ python3 stdin.py
Line1
Line 2
**END**
Line 1
Line 2

Our lines variable looks like this: "Line1\nLine2"

Here when we run our program, it waits until it we pass some data through the console window. We specify end of input using ctrl+z on windows and I believe ctrl+d on linux.

The sys.stdin.read() function also has an optional parameter for the size of the data we want to read. For example if we pass 10 then it reads 10 characters including any newline characters.

The read() function will read everything, or the size of data specified, and return it as one string. This is useful for small amounts of data but if we read large files this way, it can use up a lot of memory.

The second way is sys.stdin.readline() which is self explanatory and reads a single line from standard input with a newline character at the end.

line = sys.stdin.readline()
print(line)

$ python3 stdin.py
hello
hello

The next way is sys.stdin.readlines(). I find myself using this way most often. With this way, we read lines from the console and are returned a list containing all the lines we entered.

lines = sys.stdin.readlines()
print(lines)

$ python3 stdin.py
line1
line2
line3
['line1\n', 'line2\n', 'line3\n']

This is very useful if we wish to process a file line by line although, we do have a large list sitting in memory which we may not want with large files. I will show you how to read from files in a moment.

Reading from files:

To read from a file we can do this a couple of ways, we can open and read the file within our program.

with open('FILENAME', [rw]) as our_file:
    for line in our_file:
        print(line)

The optional [rw] specifies whether we wish to open the file for reading, r or writing, w. This will work depending on the access permission on the file. You can check this on linux from the command line by navigating to your directory where the file is and typing:

$ ls -l

This will display the access permissions of the file in that directory.

An error will be thrown if you try to read or write without having permission to do so.

If the file name you entered doesn't exist, an empty file will be created for you.

The use of with open() here is very useful as it closes our file for us when we are finished.

Another way to read a file is passing it at the command line

$ python3 stdin.py < FILENAME.txt

Presuming FILENAME.txt looks like this:

Line 1
Line 2
Line 3

Running the following program, we get the following output:

import sys

lines = sys.stdin.readlines()
print(lines)

$ python3 stdin.py < FILENAME.txt
['Line 1\n', 'Line 2\n', 'Line 3']

I dont want to talk to much about the different ways of reading and writing files as I only wanted to talk about the different methods we have available to use for reading so I wont discuss any further ways of reading.

If we wish to strip the newline characters from our lines we can use the strip() method, I'm going to use a list comprehension here as it is a good example of their usage:

lines = [line.strip() for line in sys.stdin.readlines()]
print(lines)

$ python3 stdin.py < FILENAME.txt
['Line 1', Line 2', 'Line 3']

Whats the list comprehension doing? It uses a for loop to loop through each line in standard input, takes each line and strips it then appends it to our list, lines.

Now our newline characters are gone.

We covered a fair bit of stuff here and got the chance to see some extra things in use such as list comprehensions. If you found anything here confusing, play around with it yourself, after all its one of the best ways to learn.

🌐
UltaHost
ultahost.com › knowledge-base › read-from-stdin-python
How to Read From stdin in Python | Ultahost Knowledge Base
March 14, 2025 - This approach reads all lines of input and returns them as a list, making it suitable for processing input data line by line. If handling large datasets that require bulk input, sys.stdin.read() is a better choice.
🌐
GitHub
gist.github.com › fyears › 4161739
python stdin example · GitHub
to use this I would run it like this $ echo "Your Text or Cat the file whatever" | python solnput.py <<o/p>> ... Concrete example where data flows into script using stdin from some previous program (another script). Line by line explanations for code below can be found here · # stdin.py sys.stdout = fsock print("15\n" "A\n" "B\n" "C\n" "D\n" "E\n" "F\n" "G\n" "H") sys.stdout = saveout fsock.close() # stdin.py def read_in(): return {x.strip() for x in sys.stdin} def main(): lines = read_in() for line in lines: print(line) if __name__ == '__main__': main()
🌐
Linux Hint
linuxhint.com › read-from-stdin-in-python
How to Read from stdin in Python – Linux Hint
Many ways exist in python to read from the standard input. The input() function is the most common way is to read from the standard input, which is a built-in function. The sys.stdin is another way is to read from the standard input the calls input() function internally.
🌐
Python Morsels
pythonmorsels.com › reading-from-standard-input
Reading from standard input - Python Morsels
October 30, 2023 - Python is waiting for the user of our program to type a line of text. So if we type something, and then we hit Enter, we'll see that the readline method call returned what we typed: >>> input_text = sys.stdin.readline() This is me typing >>> input_text 'This is me typing\n'
🌐
Python Pool
pythonpool.com › home › blog › best ways to read input in python from stdin
Best Ways to Read Input in Python From Stdin - Python Pool
August 19, 2022 - Python read input from stdin. What is stdin? Methods to read input from stdin: using 'input()', using sys module, using fileinput module
🌐
Delft Stack
delftstack.com › home › howto › python › how to read input from stdin in python
How to Read Input From stdin in Python | Delft Stack
March 11, 2025 - The simplest and most commonly used method for reading input from stdin in Python is the input() function. This built-in function allows you to prompt the user for input, which can then be processed as needed.
🌐
Finxter
blog.finxter.com › home › learn python blog › read from stdin in python
Read from Stdin in Python – Be on the Right Side of Change
February 4, 2022 - This is line 1. Hello and Welcome to Finxter! ... import sys # sys.stdin.readline() for i in range(int(sys.argv[1])): sys.stdin.readline() # It takes 0.25μs per iteration. # inut() for i in range(int(sys.argv[1])): input() #It is 10 times slower than the sys.readline(). In this tutorial, we looked at three different methods to read from stdin in Python: