Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #16485 > unrolled thread

Reading twice from STDIN

Started byjanedenone <janedenone@googlemail.com>
First post2011-12-01 02:01 -0800
Last post2011-12-02 07:27 -0800
Articles 7 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  Reading twice from STDIN janedenone <janedenone@googlemail.com> - 2011-12-01 02:01 -0800
    Re: Reading twice from STDIN Gelonida N <gelonida@gmail.com> - 2011-12-01 11:41 +0100
    Re: Reading twice from STDIN Dan Stromberg <drsalists@gmail.com> - 2011-12-01 18:46 -0800
      Re: Reading twice from STDIN Hans Mulder <hansmu@xs4all.nl> - 2011-12-02 08:53 +0100
        Re: Reading twice from STDIN janedenone <janedenone@googlemail.com> - 2011-12-02 01:09 -0800
          Re: Reading twice from STDIN Hans Mulder <hansmu@xs4all.nl> - 2011-12-02 14:04 +0100
            Re: Reading twice from STDIN janedenone <janedenone@googlemail.com> - 2011-12-02 07:27 -0800

#16485 — Reading twice from STDIN

Fromjanedenone <janedenone@googlemail.com>
Date2011-12-01 02:01 -0800
SubjectReading twice from STDIN
Message-ID<6de9515a-17b2-40a3-a113-5733cad9f794@cu3g2000vbb.googlegroups.com>
Hi,

I would like to read from a pipe, parse the input and ask the user
what to do next:

message = sys.stdin.read()
# message is parsed and URLs are printed as a list to choose from...
selected_index = raw_input('Which URL to open?')

Calling raw_input() always raises in an EOFError. I tried reopening
and resetting sys.stdin, but nothing worked (at least on OX X 10.7,
see http://stackoverflow.com/questions/8034595/python-raw-input-following-sys-stdin-read-throws-eoferror).

I am surprised to find that a seemingly trivial task cannot be
accomplished with Python 2.7 on a current Mac. Or am I missing
something simple?

- Jan

[toc] | [next] | [standalone]


#16489

FromGelonida N <gelonida@gmail.com>
Date2011-12-01 11:41 +0100
Message-ID<mailman.3194.1322736307.27778.python-list@python.org>
In reply to#16485
On 12/01/2011 11:01 AM, janedenone wrote:
Hi,


> 
> I would like to read from a pipe, parse the input and ask the user
> what to do next:
> 
> message = sys.stdin.read()

With above line you said, that you want to read ALL data from stdin, so
it's obvious that any following command will be unable to reda anything
from standartd in Thus the EOF error.


If you want to get input you had to read directly from the console (tty)
and NOT from stdin. Stdin has already been consumed.

It is possible in python to get the tty related to your console window
and read from it.
Unfortunately I don't kno whte commands by heart and I don't have time
now to look it up.

Perhaps somebody else can point you in the right direction.
If not I'll it up till tomorrow.



[toc] | [prev] | [next] | [standalone]


#16520

FromDan Stromberg <drsalists@gmail.com>
Date2011-12-01 18:46 -0800
Message-ID<mailman.3212.1322793980.27778.python-list@python.org>
In reply to#16485
On 12/1/11, janedenone <janedenone@googlemail.com> wrote:
> Hi,
>
> I would like to read from a pipe, parse the input and ask the user
> what to do next:
>
> message = sys.stdin.read()
> # message is parsed and URLs are printed as a list to choose from...
> selected_index = raw_input('Which URL to open?')
>
> Calling raw_input() always raises in an EOFError. I tried reopening
> and resetting sys.stdin, but nothing worked (at least on OX X 10.7,
> see
>

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.

http://stackoverflow.com/questions/8034595/python-raw-input-following-sys-stdin-read-throws-eoferror).
>
> I am surprised to find that a seemingly trivial task cannot be
> accomplished with Python 2.7 on a current Mac. Or am I missing
> something simple?
>
> - Jan
> --
> http://mail.python.org/mailman/listinfo/python-list
>

[toc] | [prev] | [next] | [standalone]


#16532

FromHans Mulder <hansmu@xs4all.nl>
Date2011-12-02 08:53 +0100
Message-ID<4ed883e4$0$6920$e4fe514c@news2.news.xs4all.nl>
In reply to#16520
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

[toc] | [prev] | [next] | [standalone]


#16534

Fromjanedenone <janedenone@googlemail.com>
Date2011-12-02 01:09 -0800
Message-ID<321b6b8b-16b2-433e-9bfc-b5ec22e77ff6@i6g2000vbe.googlegroups.com>
In reply to#16532
On Dec 2, 8:53 am, Hans Mulder <han...@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

[toc] | [prev] | [next] | [standalone]


#16541

FromHans Mulder <hansmu@xs4all.nl>
Date2011-12-02 14:04 +0100
Message-ID<4ed8cceb$0$6931$e4fe514c@news2.news.xs4all.nl>
In reply to#16534
On 2/12/11 10:09:17, janedenone wrote:
> I had tried
>
> sys.stdin = open('/dev/tty', 'r')

That seems to work for me.  This code:

import sys

if sys.version_info.major == 2:
     input = raw_input

for tp in enumerate(sys.stdin):
     print("%d: %s" % tp)

sys.stdin = open('/dev/tty', 'r')
answer = input('What is the carrying capacity of a swallow? ')
print("You answered: %s" % answer)
print("File descriptor is %d" % sys.stdin.fileno())

... does what I expect it to do in both Python 2.7 and Python 3.2.

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

That's odd.  For some reason, I can get away with a simple

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

and raw_input will merrily read from file descriptor 3.

I'm using Pyhton 2.7.1 and 3.2 on MacOS/X 10.5.0.

What version are you using?

-- HansM



[toc] | [prev] | [next] | [standalone]


#16547

Fromjanedenone <janedenone@googlemail.com>
Date2011-12-02 07:27 -0800
Message-ID<f85298af-66d2-4685-ae08-12caf312f240@z1g2000vbx.googlegroups.com>
In reply to#16541
On Dec 2, 2:04 pm, Hans Mulder <han...@xs4all.nl> wrote:

> That's odd.  For some reason, I can get away with a simple
>
>      sys.stdin = open('/dev/tty')
>
> and raw_input will merrily read from file descriptor 3.
>
> I'm using Pyhton 2.7.1 and 3.2 on MacOS/X 10.5.0.
>
> What version are you using?
>
> -- HansM

Python 2.7.1 and 3.2.2 on Mac OS X 10.7.2.

But what's really strange – you're right. It works now. I have no idea
what I did before, but I can confirm that the following code executes
flawlessly:

message = sys.stdin.read()
urls = re.findall(r'(?:http|www)\S+', message)

sys.stdin = open('/dev/tty')
# Asks user to select a URL only if more than one URL is present
if len(urls) > 1:
	for index, url in enumerate(urls):
		print '{0}: {1}\n'.format(index, url)

	selected_index = raw_input('Which URL to open? ')
	selected_index = int(selected_index)
else:
	selected_index = 0
selected_url = urls[selected_index]

Quite embarrassing. In any case, I am happy to have found the simplest
solution.

Thanks again,
Jan

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web