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().

🌐
GeeksforGeeks
geeksforgeeks.org › python › difference-between-input-and-sys-stdin-readline
Difference between input() and sys.stdin.readline() - GeeksforGeeks
July 12, 2025 - More this method also provides the parameter for the size i.e. how many characters it can read at a time. Example: ... # Python program to demonstrate # sys.stdin.readline() import sys name = sys.stdin.readline() print(name) num = sys.stdin.readline(2) print(num)
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
Standard idiom for reading from stdin, writing to stdout?
I have a bunch of small Python scripts meant to read from stdin and write to stdout if input and/or output files aren’t given, you know, the usual Unix pipeline idiom. Note that I’m not piping within the program, just reading from stdin, writing to stdout (so packages like the pipe package ... More on discuss.python.org
🌐 discuss.python.org
12
0
January 12, 2025
Handling `sys.stdin.read()` in Non-Blocking Mode - Core
Hello Python Community, I’m seeking your input on an issue related to the behavior of sys.stdin.read() when stdin is set to non-blocking mode and no input is available. This discussion is based on issue #109523 in the Python GitHub repository. During the sprint at EuroPython 2024, I created ... More on discuss.python.org
🌐 discuss.python.org
5
July 31, 2024
What's the difference between sys.stdin.read() and input()?
The reason it's so long is that we just imported the library, so we have to reference sys at the beginning of any function from that library, then .stdin() is a function with a .read() method available in it (among others) - so it wouldn't make sense to just say read() without telling Python which ... More on teamtreehouse.com
🌐 teamtreehouse.com
2
November 27, 2015
🌐
Stack Abuse
stackabuse.com › reading-from-stdin-in-python
Reading from stdin in Python
August 28, 2023 - In this code, we simply use stdin's readline() method to get input from the user. The line is then printed out with the prefix "Received: ". If you run this code and type in some input, you'll see that it echoes back each line you enter: $ python read_stdin.py Hello, world!
🌐
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 program saves the input from stdin into a variable (name) and returns the value. The fileinput Python library contains the input() function, which allows reading file names from standard input.
🌐
GeeksforGeeks
geeksforgeeks.org › python › take-input-from-stdin-in-python
Take input from stdin in Python - GeeksforGeeks
July 12, 2025 - There are a number of ways in which we can take input from stdin in Python.
🌐
DigitalOcean
digitalocean.com › community › tutorials › read-stdin-python
How to Read from stdin in Python | DigitalOcean
August 3, 2022 - Hi Processing Message from sys.stdin ... so that we can check if the user has entered “Exit” message or not. We can also use Python input() function to read the standard input data....
Find elsewhere
🌐
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:
🌐
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.
🌐
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 - If you prefer a more concise way to read input from stdin in Python, you can use the open() function to create a file object that represents stdin, and then use the read() method to read all the input at once as a string.
🌐
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.
🌐
AskPython
askpython.com › home › python – stdin, stdout, and stderr
Python - stdin, stdout, and stderr - AskPython
February 16, 2023 - Python’s sys module provides us with all three file objects for stdin, stdout, and stderr. For the input file object, we use sys.stdin. This is similar to a file, where you can open and close it, just like any other file. ... import sys stdin_fileno = sys.stdin # Keeps reading from stdin and quits only if the word 'exit' is there # This loop, by default does not terminate, since stdin is open for line in stdin_fileno: # Remove trailing newline characters using strip() if 'exit' == line.strip(): print('Found exit.
🌐
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.
🌐
Python.org
discuss.python.org › core development
Handling `sys.stdin.read()` in Non-Blocking Mode - Core
July 31, 2024 - Hello Python Community, I’m seeking your input on an issue related to the behavior of sys.stdin.read() when stdin is set to non-blocking mode and no input is available. This discussion is based on issue #109523 in the P…
🌐
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'
Top answer
1 of 2
15
Hi Brendan, in this video the "sys.stdin.read()" is described as being able to take a newline and finish your entry with Control+D. input() would finish your entry with the "Enter" key being pressed on your keyboard, so you couldn't include a newline in your data input that way.
2 of 2
6
That sounds roughly correct, however input() also takes as an argument a string to use as a prompt, while sys.stdin.read() takes the length to read into the user-entered string as an optional argument instead (and provides no prompt - in the video, a print() was provided to serve as a prompt instead). For more information on what these functions are doing though, you can use help(sys.stdin.read) and help(input) while in a Python interpreter, or visit https://docs.python.org/2/library/sys.html for more information about the sys library and its methods, including stdin. As for your other question, we have to import the sys library because sys.stdin.read() is reflecting a method that exists only in that library. The reason it's so long is that we just imported the library, so we have to reference sys at the beginning of any function from that library, then .stdin() is a function with a .read() method available in it (among others) - so it wouldn't make sense to just say read() without telling Python which read() method you're asking it to use (other functions, including one you write yourself, could include their own read() methods). If you mean to say why sys is a library instead of being ready for use in Python all the time, that's likely because it would be inefficient for Python to keep libraries loaded if they aren't being used, so the library is kept optional.
🌐
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
🌐
Quora
quora.com › How-do-I-take-input-from-STDIN-in-Python
How to take input from STDIN in Python - Quora
So to do so, we use the ‘stdin.readline()’ function in ‘sys’ library.