Sys.stdin not closing on ctrl-d

I have the following code in program.py:

from sys import stdin
for line in stdin:
    print line

I launched, enter the lines, and then press Ctrl+ D, but the program will not exit.

It works:

$ printf "echo" | python program.py 

Why doesn't the program exit when I press Ctrl+ D? I am using Fedora 18 terminal.

+5
source share
1 answer

Ctrl+ Dhas a weird effect. It does not close the input stream, but only causes the C-level to fread()return an empty result. For regular files, this result means that the file is at its end, but it is acceptable to read more, for example. to check if someone has added more data to the file.

, - !

  • Python . .

  • stdin C .

  • (!) ( " " ) ​​, , Ctrl + D - .

, , , , - Ctrl + D, :

import sys

while True:
   line = sys.stdin.readline()
   print repr(line)

'\n', , "" Ctrl + D, '' ( , , , if line == '': break).

+13

All Articles