为什么我必须键入ctrl -d两次? [英] Why do I have to type ctrl-d twice?

查看:191
本文介绍了为什么我必须键入ctrl -d两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于我自己的娱乐,我已经编写了一个python脚本,该脚本使我可以将python用于bash单线;提供python生成器表达式;然后脚本对其进行迭代。这是脚本:

For my own amusement, I've cooked up a python script that allows me to use python for bash one-liners; Supply a python generator expression; and the script iterates over it. Here's the script:

DEFAULT_MODULES = ['os', 're', 'sys']

_g = {}
for m in DEFAULT_MODULES:
    _g[m] = __import__(m)

import sys
sys.stdout.writelines(eval(sys.argv[1], _g))

这就是您可能会使用它的方式。

And here's how you might use it.

$ groups | python pype.py '(l.upper() for l in sys.stdin)'
DBORNSIDE
$ 

对于预期用途,它可以完美地工作!

For the intended use, it works perfectly!

但是,当我不使用管道来填充它而直接调用它时,例如: [强调是为了显示我键入的内容]

But when I don't feed it with pipe and just invoke it directly, for instance: [emphasis added to show what I type]


$ python pype.py '("%r\n" % (l,) for l in sys.stdin)'
fooEnter
barEnter
bazEnter
Ctrl DCtrl D'foo\n'
'bar\n'
'baz\n'
$ 

为了停止接受输入并产生任何输出,我有输入 Enter - Ctrl D - Ctrl D Ctrl D - Ctrl D - Ctrl D 。这违反了我的期望,应该按输入内容处理每一行,并且随时输入 Ctrl D 会结束脚本。我的理解力在哪里?

In order to stop accepting input and produce any output, I have to type either Enter - Ctrl D - Ctrl D or Ctrl D - Ctrl D - Ctrl D. This violates my expectations, that each line should be processed as entered, and that typing Ctrl D at any time will end the script. Where is the gap in my understanding?

编辑:我更新了交互式示例,以表明我在他的回答中看不到wim所描述的引文,还有一些

I've updated the interactive example to show that I'm not seeing the quoting wim describes in his answer, and some more examples too.


$ python pype.py '("%r\n" % (l,) for l in sys.stdin)'
fooCtrl DCtrl DbarEnter
Ctrl DCtrl D'foobar\n'
$ python pype.py '("%r\n" % (l,) for l in sys.stdin)'
fooCtrl VCtrl D^DbarEnter
Ctrl DCtrl D'foo\x04bar\n'
$ 


推荐答案

Ctrl-D 不一定识别为EOF,而是识别为终止当前 read()呼叫。

Ctrl-D is recognized not necessarily as EOF, but as "terminate current read() call".

如果您有空行(或仅按 Ctrl-D ),然后按 Ctrl -D ,您的 read()立即终止,并返回0个读取字节。

If you have an empty line (or just pressed Ctrl-D) and press Ctrl-D, your read() terminates immediately and returns 0 read bytes. And this is a sign for EOF.

如果一行中有数据并按 Ctrl-D ,则您的 read()终止于任何键入的内容,当然没有终止换行符('\n')。

If you have data in a line and press Ctrl-D, your read() terminates with whatever there has been typed, of course without a terminating newline ('\n').

因此,如果您有输入数据,请按两次 Ctrl-D 两次非空行,或者在空行中按一次,即使用 Enter 之前。

So if you have input data, you press Ctrl-D twice of a non-empty line or once on a empty one, i.e. with Enter before.

所有这些都适用于普通的操作系统界面,可通过 os.read()。

This all holds for the normal OS interface, accessible from Python via os.read().

Python文件对象以及文件迭代器,会将第一个EOF视为当前 read()打电话,因为他们认为已经没有了。下一个 read()调用将再次尝试,并且需要另一个 Ctrl-D 才能真正返回0字节。原因是文件对象 read()总是尝试返回所请求的字节数,并在操作系统 read()<时尝试填满/ code>返回的值小于请求的值。

Python file objects, and also file iterators, treat the first EOF recognized as termination for the current read() call, as they suppose there is nothing any longer. A next read() call tries again and needs another Ctrl-D in order to really return 0 bytes. The reason is that a file object read() always tries to return as many bytes as requested and tries to fill up if a OS read() returns less than requested.

file.readline()相反, iter(file)使用内部的 read()函数进行读取,因此总是对额外的有特殊要求Ctrl-D

As opposite to file.readline(), iter(file) uses the internal read() functions to read and thus always has this special requirement of the extra Ctrl-D.

我总是使用 iter(file.readline,'')进行读取从文件中逐行输入。

I always use iter(file.readline, '') to read line-wise from a file.

这篇关于为什么我必须键入ctrl -d两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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