Reading twice from STDIN

janedenone janedenone at googlemail.com
Fri Dec 2 04:09:17 EST 2011


On Dec 2, 8:53 am, Hans Mulder <han... at xs4all.nl> wrote:
> On 2/12/11 03:46:10, Dan Stromberg wrote:
>
> > You can read piped data from sys.stdin normally.  Then if you want
> > something from the user, at least on most *ix's, you would open
> > /dev/tty and get user input from there.  'Not sure about OS/X.
>
> Reading from /dev/tty works fine on OS/X.
>
> -- HansM

Many thanks for the pointers – I had tried

sys.stdin = open('/dev/tty', 'r')

but the actual solution is slightly more complicated. With your help
(and another round of Google searches), I found exactly what I was
looking for:

http://turambar.org/jarkko/stdin_input.php

Because raw_input() (input() in Python 3) reads from file descriptor
0, the solution is to copy this file descriptor elsewhere, and use a
file descriptor pointing to /dev/tty for the user input.

Thanks again - Jan



More information about the Python-list mailing list