Search code examples
pythoninputinteractivesys

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


Solution

  • For UNIX based systems (Linux, Mac):

    Hello, you can type : 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, type Ctrlz