如何从命令行将EOF发送到Python sys.stdin? CTRL-D不起作用 [英] How to send EOF to Python sys.stdin from commandline? CTRL-D doesn't work

查看:120
本文介绍了如何从命令行将EOF发送到Python sys.stdin? CTRL-D不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从UNIX上的命令行写入我的Python进程。我想发送EOF(或以某种方式刷新stdin缓冲区,以便Python可以读取我的输入。)

I am writing to my Python process from the commandline on unix. I want to send EOF (or somehow flush the stdin buffer, so Python can read my input.)

如果我按CTRL-C,则会收到KeyboardError。

If I hit CTRL-C, I get a KeyboardError.

如果我按CTRL-D,程序将停止。

If I hit CTRL-D, the program just stops.

如何刷新标准输入缓冲区?

How do I flush the stdin buffer?

推荐答案

如果在sys.stdin中使用 for l,它将被缓冲。

If you use 'for l in sys.stdin', it is buffered.

您可以使用:

  while 1:
     l = sys.stdin.readline()

这篇关于如何从命令行将EOF发送到Python sys.stdin? CTRL-D不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆