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.)
If I hit CTRL-C, I get a KeyboardError.
If I hit CTRL-D, the program just stops.
How do I flush the stdin buffer?
Control-D should NOT make your program "just stop" -- it should close standard input, and if your program deals with that properly, it may perfectly well continue if it needs to!
For example, given the following
st.py
:we could see something like
if the control-D is hit right after the enter on the third line -- the program realizes that standard input is done, and performs the needed post-processing, all neat and proper.
If your program prematurely exits on control-D, it must be badly coded -- what about editing you question to add the smallest "misbehaving" program you can conceive of, so we can show you exactly HOW you're going wrong?
I think I know what's happening. You are hitting
ctrl-D
without hittingenter
. If you want to send a line to the program, just hit enter. If you hitctrl-D
without hittingenter
, you can hitctrl-D
again and your program should see the line then. In this case (twoctrl-D
s in succession), your program will not see a newline at the end of the line.For example, let's say I have a Python script
a.py
:And I execute it:
And then enter the following:
the program will print:
$
is the shell-prompt. Here's a full session with the above input:$ python a.py
line 1
line 2 line1
line 2$
(Bold show the program's output. Roman-case is for showing what I typed, sans the two
ctrl-D
s)If this is not what's happening, you need to tell us more about what you are doing.
If you use 'for l in sys.stdin', it is buffered.
You can use: