I'm trying to do some of the code golf challenges, but they all require the input to be taken from stdin. How do I get that in Python?
Join Stack Overflow to learn, share knowledge, and build your career.
|
|
|
Here's from Learning Python:
On Unix, you could test it by doing something like:
On Windows or DOS, you'd do:
|
|||||||||||||||||
|
|
The problem I have with solution
is that if you don't pass any data to stdin, it will block forever. That's why I love this answer: check if there is some data on stdin first, and then read it. This is what I ended up doing:
|
|||||
|
You just need to TLDR: compatible with Python 2 and 3, Windows, UnixSay you have a file,
Longer answerHere's a complete, easily replicable demo, using two methods, the builtin function, To begin with, let's create a file for inputs:
And using the code we've already seen, we can check that we've created the file:
Here's the help on
Builtin function,
|
|
Python also has built-in functions For example,
or
|
|||||
|
|
One option is to use input function as shown below. There is also raw_input method if you are not sure about the data type.
Running the program:
Reference: http://www.python-course.eu/input.php |
|||||
|
|
I am pretty amazed no one had mentioned this hack so far:
compatible with both python2 and python3 |
||||
|
|
|
Regarding this:
I just tried it on python 2.7 (following someone else's suggestion) for a very large file, and I don't recommend it, precisely for the reasons mentioned above (nothing happens for a long time). I ended up with a slightly more pythonic solution (and it works on bigger files):
Then I can run the script locally as:
|
|||||||||||||||||||||
|
|
Read from The solution is to set mode to binary if Windows + Python 2 is detected, and on Python 3 use
|
|||
|
|
|
There's a few ways to do it.
You will probably find this Wikibook article on I/O in Python to be a useful reference as well. |
|||||||||||||
|
|
The following chip of code will help you :
|
|||
|
|
|
You could use the
|
|||||||||
|
|
Building on all the anwers using
and use it as either
or
or even
That would make your Python script behave like many GNU/Unix programs such as |
||||
|
|
|
You can read from stdin and then store inputs into "data" as follows:
|
|||||
|
|
I had some issues when getting this to work for reading over sockets piped to it. When the socket got closed it started returning empty string in an active loop. So this is my solution to it (which I only tested in linux, but hope it works in all other systems)
So if you start listening on a socket it will work properly (e.g. in bash):
And you can call it with telnet or just point a browser to localhost:12345 |
|||
|
|
|
The answer proposed by others:
is very simple and pythonic, but it must be noted that the script will wait until EOF before starting to iterate on the lines of input. This means that The correct script for such a use case would be:
UPDATE |
|||||||||||||||||||||
|
|
Try this:
and check it with:
|
|||
|
|
|
This will echo standard input to standard output:
|
||||
|
|
|
||||
|
|
protected by Kev Sep 22 '12 at 13:46
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?