Reading twice from STDIN

janedenone janedenone at googlemail.com
Fri Dec 2 10:27:43 EST 2011


On Dec 2, 2:04 pm, Hans Mulder <han... at 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



More information about the Python-list mailing list