How to finish sys.stdin.readlines() input?

Gandi picture Gandi · Apr 5, 2011 · Viewed 59.4k times · Source

This might be a silly question, but as I can't find an answer, I have to ask it.

In interactive python I want to process a message which i get with:

>>> message = sys.stdin.readlines()

Everything works fine, but... how to stop it from getting an input and make it save into message variable? Stopping with ctrl+c stops whole process so there is no input to be saved anywhere. I guess there's an easy answer I just can't find...

Answer

Sandro Munda picture Sandro Munda · Apr 5, 2011

For unix based system :

Hello, you can tape : Ctrld

Ctrld closes the standard input (stdin) by sending EOF.

Example :

>>> import sys
>>> message = sys.stdin.readlines()
Hello
World
My
Name
Is
James
Bond
# <ctrl-d> EOF sent
>>> print message
['Hello\n', 'World\n', 'My\n', 'Name\n', 'Is\n', 'James\n', 'Bond\n']

For Windows :

To send EOF on Windows, you can replace Ctrld by Ctrlz