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

🌐
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
🌐
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.
Discussions

python - How do I read from stdin? - Stack Overflow
Python also has built-in functions ... See the Python documentation under Built-in Functions. ... This reads a single line, which isn't really what the OP asked about. I interpret the question as "how do I read a bunch of lines from an open file handle until EOF?" 2015-12-22T08:51:49.37Z+00:00 ... The OP isn't asking to read input from a keyboard, He is asking to read from stdin which in a ... More on stackoverflow.com
🌐 stackoverflow.com
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
linux - How to pass stdin to python script - Unix & Linux Stack Exchange
since $ python cd_alias_test.py hello would print all the args: ['test.py', 'hello'] I'd expect this alias to do the same. However its stdout is ... Which means that the input string is being passed to stdin and not the script's arguments. More on unix.stackexchange.com
🌐 unix.stackexchange.com
June 23, 2022
🌐
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.
🌐
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.
🌐
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.
Find elsewhere
🌐
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.
🌐
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 ...
🌐
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 don’t provide a prompt ... no visible prompt on the console. The stdin is a variable in the sys module in Python that can be used to read from the console or stdin....
🌐
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.
🌐
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.
🌐
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!
🌐
Medium
medium.com › @pies052022 › python-read-input-from-stdin-methods-with-examples-42fcfff54970
Python Read Input From Stdin Methods with Examples | by JOKEN VILLANUEVA | Medium
November 19, 2025 - Python Read Input From Stdin Methods with Examples This (stdin) is a standard input that is used as an interpreter to read input from the user. In Python, to read input from stdin, it could simply be …
🌐
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.
🌐
Python Morsels
pythonmorsels.com › reading-from-standard-input
Reading from standard input - Python Morsels
October 30, 2023 - Python also includes a readable file-like object for prompting users for input. This file-like object is called standard input: >>> sys.stdin <_io.TextIOWrapper name='<stdin>' mode='r' encoding='utf-8'>