So you have used Python's "pre built in functions", presumably like this:

file_object = open('filename')
for something in file_object:
    some stuff here

This reads the file by invoking an iterator on the file object which happens to return the next line from the file.

You could instead use:

file_object = open('filename')
lines = file_object.readlines()

which reads the lines from the current file position into a list.

Now, sys.stdin is just another file object, which happens to be opened by Python before your program starts. What you do with that file object is up to you, but it is not really any different to any other file object, its just that you don't need an open.

for something in sys.stdin:
    some stuff here

will iterate through standard input until end-of-file is reached. And so will this:

lines = sys.stdin.readlines()

Your first question is really about different ways of using a file object.

Second, where is it reading from? It is reading from file descriptor 0 (zero). On Windows it is file handle 0 (zero). File descriptor/handle 0 is connected to the console or tty by default, so in effect it is reading from the keyboard. However it can be redirected, often by a shell (like bash or cmd.exe) using syntax like this:

myprog.py < input_file.txt 

That alters file descriptor zero to read a file instead of the keyboard. On UNIX or Linux this uses the underlying call dup2(). Read your shell documentation for more information about redirection (or maybe man dup2 if you are brave).

Answer from cdarke on Stack Overflow
🌐
Python
docs.python.org › 3 › library › sys.html
sys — System-specific parameters and functions
They are used during finalization, and could be useful to print to the actual standard stream no matter if the sys.std* object has been redirected. It can also be used to restore the actual files to known working file objects in case they have been overwritten with a broken object. However, the preferred way to do this is to explicitly save the previous stream before replacing it, and restore the saved object. ... Under some conditions stdin, stdout and stderr as well as the original values __stdin__, __stdout__ and __stderr__ can be None.
Top answer
1 of 6
49

So you have used Python's "pre built in functions", presumably like this:

file_object = open('filename')
for something in file_object:
    some stuff here

This reads the file by invoking an iterator on the file object which happens to return the next line from the file.

You could instead use:

file_object = open('filename')
lines = file_object.readlines()

which reads the lines from the current file position into a list.

Now, sys.stdin is just another file object, which happens to be opened by Python before your program starts. What you do with that file object is up to you, but it is not really any different to any other file object, its just that you don't need an open.

for something in sys.stdin:
    some stuff here

will iterate through standard input until end-of-file is reached. And so will this:

lines = sys.stdin.readlines()

Your first question is really about different ways of using a file object.

Second, where is it reading from? It is reading from file descriptor 0 (zero). On Windows it is file handle 0 (zero). File descriptor/handle 0 is connected to the console or tty by default, so in effect it is reading from the keyboard. However it can be redirected, often by a shell (like bash or cmd.exe) using syntax like this:

myprog.py < input_file.txt 

That alters file descriptor zero to read a file instead of the keyboard. On UNIX or Linux this uses the underlying call dup2(). Read your shell documentation for more information about redirection (or maybe man dup2 if you are brave).

2 of 6
8

It is reading from the standard input - and it should be provided by the keyboard in the form of stream data.

It is not required to provide a file, however you can use redirection to use a file as standard input.

In Python, the readlines() method reads the entire stream, and then splits it up at the newline character and creates a list of each line.

lines = sys.stdin.readlines()

The above creates a list called lines, where each element will be a line (as determined by the end of line character).

You can read more about this at the input and output section of the Python tutorial.

If you want to prompt the user for input, use the input() method (in Python 2, use raw_input()):

user_input = input('Please enter something: ')
print('You entered: {}'.format(user_input))
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
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
What Are the Features of the sys.stdin.read() Method for Input in Python?
home / developersection / forums / what are the features of the sys.stdin.read() method for input in python? More on mindstick.com
🌐 mindstick.com
0
April 2, 2025
🌐
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")
🌐
DigitalOcean
digitalocean.com › community › tutorials › read-stdin-python
How to Read from stdin in Python | DigitalOcean
August 3, 2022 - import sys for line in sys.stdin: if 'Exit' == line.rstrip(): break print(f'Processing Message from sys.stdin *****{line}*****') print("Done")
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.
Find elsewhere
🌐
Runebook.dev
runebook.dev › en › docs › python › library › sys › sys.__stdin__
python - Understanding and Restoring sys.stdin with sys.stdin
October 21, 2025 - sys.stdin This is the standard input stream that the Python interpreter uses for functions like input(). Crucially, it can be reassigned (redirected) to another file-like object.
🌐
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'>
🌐
Sentry
sentry.io › sentry answers › python › read user input (stdin) in python
Read user input (STDIN) in Python | Sentry
November 15, 2023 - This allows us to treat stdin like a file. For example: import sys for line in sys.stdin: print(f"Received: {line.strip()}") We could then pipe a file (lines.txt) into our script: python stdin_reader.py < lines.txt ·
🌐
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. This function returns a string that the user has entered on the command line. You can also use sys.stdin.read() if you want to read the entire contents of standard input as a single string.
🌐
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 - For example: ... Alternatively, you can use the sys.stdin object, which is a file-like object that can be read from using the standard file I/O methods, such as read(), readline(), and readlines().
🌐
GeeksforGeeks
geeksforgeeks.org › python › difference-between-input-and-sys-stdin-readline
Difference between input() and sys.stdin.readline() - GeeksforGeeks
July 12, 2025 - Stdin stands for standard input which is a stream from which the program reads its input data. This method is slightly different from the input() method as it also reads the escape character entered by the user.
🌐
Stack Abuse
stackabuse.com › reading-from-stdin-in-python
Reading from stdin in Python
August 28, 2023 - Python, like most programming languages, provides a few different ways to read from stdin. We'll explore a couple of the most common methods in the following sections. The sys.stdin object in Python is a file-like object that acts as the stdin stream for the Python program.
🌐
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 using four different methods. We will discuss these methods in detail. import fileinput import sys # Using input() function input_line = input("Enter text: ") # Using sys.stdin print("Enter text (type 'q' to quit): ") for line in sys.stdin: line = line.rstrip() if line == "q": break # Using fileinput.input() print("Enter text (type 'q' to quit): ") for line in fileinput.input(): line = line.rstrip() if line == "q": break # Using open(0).read() print("Enter text (type 'q' to quit): ") text = open(0).read().rstrip() lines = text.split("\n") for line in lines: if line == "q": break
🌐
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 ...
🌐
GitHub
gist.github.com › fyears › 4161739
python stdin example · GitHub
python3 -c 'import sys; from urllib.parse import quote; print(quote(sys.stdin.readlines()[0]))'
🌐
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 - Reads user input from the keyboard (sys.stdin). Removes new lines from the user entry (rstrtip()). Prints back the entered message by appending an f-string. Exits the for loop (break). The program expects user input, prints the entered text stream, and returns to the command line. The second way to read input from stdin in Python is with the built-in input() function.
🌐
MindStick
mindstick.com › forum › 161314 › what-are-the-features-of-the-sys-stdin-read-method-for-input-in-python
What Are the Features of the sys.stdin.read() Method for Input in Python? – MindStick
April 2, 2025 - The read method from the sys.stdin module in Python allows reading continuous input streams coming from standard input (stdin). The sys.stdin.read() function is suitable for three standard input situations: command-line redirection and piping, ...