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

🌐
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 - Stdin reads keyboard inputs from a user, files, or data streams. Python offers several different methods to read from stdin depending on the type of problem.
🌐
GitHub
gist.github.com › fyears › 4161739
python stdin example · GitHub
# 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() In the console, run this to see above working. Why this works is also explained here · >>>Python3.5 stdout.py >>>cat out.log | Python3.5 stdin.py
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
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
How to read 1MB of input from stdin?
Python sys.stdin.buffer.read() exits when input to stdin is greater than 873816 length. 1MB is 1048576. More on discuss.python.org
🌐 discuss.python.org
0
0
January 7, 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
🌐
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.
🌐
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.
🌐
CodersLegacy
coderslegacy.com › home › python › python stdin – taking input
Python stdin - Taking input - CodersLegacy
October 6, 2022 - The above technique allows you to continuously input data into the Python Program from the command prompt or IDE terminal. All you have to do is press enter for every line/word you wrote to be sent back to the program, where it will be printed out. This technique is a bit tricky, but it’s a pretty useful way of taking input. stdin can accept multiple lines, without having to type them all out first.
🌐
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 *****Hi ***** Hello Processing Message from sys.stdin *****Hello ***** Exit Done ... Notice the use of rstrip() to remove the trailing newline character 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
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.
🌐
Sentry
sentry.io › sentry answers › python › read user input (stdin) in python
Read user input (STDIN) in Python | Sentry
November 15, 2023 - Python provides a few methods for reading from stdin in different contexts. The simplest and most commonly used is the default input function, which prompts the user to enter a line into stdin and returns it as a string once they press Enter.
🌐
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!
🌐
Python.org
discuss.python.org › python help
How to read 1MB of input from stdin? - Python Help - Discussions on Python.org
January 7, 2023 - Python sys.stdin.buffer.read() exits when input to stdin is greater than 873816 length. 1MB is 1048576.
🌐
PythonHow
pythonhow.com › how › read-from-stdin-standard-input
Here is how to read from stdin (standard input) in Python
In Python, you can read from standard input (stdin) using the built-in input() function.
🌐
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 documentation
docs.python.org › 3 › tutorial › inputoutput.html
7. Input and Output — Python 3.14.4 documentation
>>> f.close() >>> f.read() Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: I/O operation on closed file. The rest of the examples in this section will assume that a file object called f has already been created. To read a file’s contents, call f.read(size), which reads some quantity of data and returns it as a string (in text mode) or bytes object (in binary mode).
🌐
Wellsr
wellsr.com › python › python-stdin-stdout-stderr-with-sys-module
Using Python stdin, stdout, and stderr with the sys Module - wellsr.com
August 9, 2019 - We can use these pipeline tools in our own Python script, like this: import sys for line in sys.stdin: # Process stdin print("[stdout output]") # Print statements go to stdout print("[stderr output]", file=sys.stderr) # (Python Ver. 3) print to stderr instead of stdout print >> sys.stderr, "[stderr output]" # (Python Ver.
🌐
Quora
quora.com › How-do-I-take-input-from-STDIN-in-Python
How to take input from STDIN in Python - Quora
Answer (1 of 19): In python - you can use * sys.stdin : A file like object which is the standard input * sys.stdout : A file like object which is the standard output They are already open when you start your application - they don’t need to ...
🌐
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
docs.python.org › 3 › library › sys.html
sys — System-specific parameters and functions
Non-console character devices such as NUL (i.e. where isatty() returns True) use the value of the console input and output codepages at startup, respectively for stdin and stdout/stderr. This defaults to the system locale encoding if the process is not initially attached to a console. The special behaviour of the console can be overridden by setting the environment variable PYTHONLEGACYWINDOWSSTDIO before starting Python.