Why do I have to press Ctrl+D twice to close stdin?

后端 未结 5 799
死守一世寂寞
死守一世寂寞 2021-01-04 05:12

I have the following Python script that reads numbers and outputs an error if the input is not a number.

import fileinput
import sys
for line in (txt.strip()         


        
5条回答
  •  萌比男神i
    2021-01-04 06:06

    I wrote an explanation about this in my answer to this question.

    How to capture Control+D signal?

    In short, Control-D at the terminal simply causes the terminal to flush the input. This makes the read system call return. The first time it returns with a non-zero value (if you typed something). The second time, it returns with 0, which is code for "end of file".

提交回复
热议问题