Flushing standard input

Fabian Steiner lists at fabis-site.net
Wed Oct 18 15:45:05 EDT 2006


Recently I came across a problem which I still can't solve on my own.
Consider this small example:

import sys
import time

time.sleep(3)
print
sys.stdin.flush()
input = raw_input('Your input: ')
print 'Your input: ', input

While the script is sleeping I type in the word 'test1', so that it is
printed on the console. Having slept for three seconds the script
continues and wants me to type in another word 'test2' and I hit return.

The output looks like this:

fabi at jupiter ~ [ 21:41:27 ] $ python test.py
test1
test2
Your input:  test1test2

Is there any way to flush the stdin buffer so that 'test1' is _not_
regarded as input? How could one solve this issue?

Cheers,
Fabian



More information about the Python-list mailing list