Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 617
Gandi Avatar asked Apr 05 '11 08:04

Gandi


People also ask

What is SYS stdin readline?

stdin. readline() Stdin stands for standard input which is a stream from which the program reads its input data. This method is slightly different from the input() method as it also reads the escape character entered by the user.

Is SYS stdin readline faster than input?

stdin. readline() is the fastest one when reading strings and input() when reading integers.


1 Answers

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

like image 183
Sandro Munda Avatar answered Sep 29 '22 18:09

Sandro Munda