sys.stdin does not close on ctrl-d

耗尽温柔 提交于 2019-12-04 14:03:40

问题


I have the following code in program.py:

from sys import stdin
for line in stdin:
    print line

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

This does work:

$ printf "echo" | python program.py 

Why does the program not exit when I press Ctrl+d? I am using the Fedora 18 terminal.


回答1:


Ctrl+D has a strange effect. It doesn't close the input stream, but only causes a C-level fread() to return an empty result. For regular files such a result means that the file is now at its end, but it's acceptable to read more, e.g. to check if someone else wrote more data to the file in the meantime.

In addition, there are issues of buffering --- three levels of them!

  • Python's iteration over a file does block buffering. Avoid it to read from interactive streams.

  • the C-level stdin file has, by default, a line buffer.

  • the terminal itself(!), in its default mode ("cooked mode"), reads one line of data before sending it to the process, which explains why typing Ctrl+D doesn't have any effect when typed in the middle of a line.

This example avoids the first issue, which is all you need if all you want is detecting Ctrl+D typed as its own line:

import sys

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

You get every line with a final '\n', apart from when the "line" comes from a Ctrl+D, in which case you get just '' (but reading continues, unless of course we add if line == '': break).



来源:https://stackoverflow.com/questions/15666923/sys-stdin-does-not-close-on-ctrl-d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!