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

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


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()HelloWorldMyNameIsJamesBond# <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


This is an old question but it needs an update about Windows and different keyboard layouts.

If neither CTRL + Z nor CTRL + D ** work for you on Windows and and you're wandering what is going on do this:

  • check if you are using default english keyboard layout
  • if you do have different, non-default keyboard layout try switching keyboard setting to English in language bar, then try pressing ctrl + z after changes
  • if you're still confused look at the screen, what appears in command line when you press ctrl + z. What symbol do you see? When I was pressing ctrl + z I was seeing this: ^Y, and when by mistake I pressed ctrl + y I've seen this ^Z, i pressed enter and the input was taken, EOF sent.

This is somewhat strange and counterintuitive. I changed keys layout some time ago to include polish characters, but all the common keys are left unchanged, z still maps to z when I use the keyboard normally, normally ctrl + z does nothing in my keyboard, so I shouldn't be changed. But apparently in cmd it works differently, in order to have default link between ctrl and z I have to switch to default layout, or use control y to sent EOF.


Use CTRL-D.

message = sys.stdin.readlines()abcdef<CTRL-D># message == ['abc\n', 'def\n']