I get how to open files, and then use Python's pre built in functions with them. But how does sys.stdin work?
for something in sys.stdin:
some stuff here
lines = sys.stdin.readlines()
What's the difference between the above two different uses on sys.stdin? Where is it reading the information from? Is it via keyboard, or do we still have to provide a file?
So you have used Python's "pre built in functions", presumably like this:
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:
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 anopen
.will iterate through standard input until end-of-file is reached. And so will this:
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:
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 maybeman dup2
if you are brave).According to me sys.stdin.read() method accepts a line as the input from the user until a special character like Enter Key and followed by Ctrl + D and then stores the input as the string.
Control + D works as the stop signal.
Example:
After running the program enter two numbers delimited by space and after finishing press Control + D once or twice and you will be presented by the sum of the two inputs.
To get a grasp how sys.stdin works do following:
create a simple python script, let's name it "readStdin.py":
Now open console any type in:
The script outputs:
So, the script has read the input into list (named 'lines'), including the new line character produced by 'echo'. That is.
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.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, useraw_input()
):The code above does not work as you expect because
sys.stdin
is a file handle - it is a file handle to thestdin
. It will not reach thesome stuff here
lineWhen the script above is run in an interactive shell, it will block the execution until a user presses Ctrl-D, which indicates the end of the input.