473,386 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Flushing standard input

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@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
Oct 18 '06 #1
2 14526
I found this.
http://mail.python.org/pipermail/pyt...er/359296.html

You cannot flush input. The flush method only relates to output. The
*other* side of the file has to flush *its* output in order for you to
see it as input.

Oct 18 '06 #2
At Wednesday 18/10/2006 16:45, Fabian Steiner wrote:
>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@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?
This works ONLY on Windows:

import sys
import time
from msvcrt import getch,kbhit

time.sleep(3)
while kbhit(): getch() # consume any pending keypresses
print
input = raw_input('Your input: ')
print 'Your input: ', input

For Unix you could look at the tty/termios modules. This recipe shows
a portable getch() function:
<http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/134892>
--
Gabriel Genellina
Softlab SRL

__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Oct 18 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Graham Ashton | last post by:
Hi. I'm having trouble flushing sys.stdout. I've written a small example to illustrate my problem (see below). In short, I expect it to ping "received hello", sleep for 5 seconds and then print...
8
by: CJM | last post by:
I have quite a large report, which takes a couple of minutes to run. I'd like to show line-by-line updates as they become available, but since I'm using IE6, Response.Flush() doesnt work. I know...
0
by: Miha | last post by:
I wrote a simple Windows Application based application in C# called the CallWebServiceGui. This application is started from a different application called WsGuiStarter. The WsGuiStarter redirects...
5
by: arnuld | last post by:
this is from mentioned section. i did not understand some things here: it means "flushing the buffer" and "writing to output device" are SAME thing, these are just 2 different names for the...
0
by: theJade | last post by:
I'm currently running Python 2.5 on Windows 2003. I'm not sure if this is strictly a python issue but I was wondering if anyone has seen and/or knows how to fix this problem. From a linux...
2
by: Devine123 | last post by:
Hi, I’m creating a perl script that takes incoming http/s requests, logs the standard input, output and error, before returning the output to the client. The input, output and error log is appended...
1
by: terminatorul | last post by:
Hello Sorry if asking a known question. I have a program that reads lines of text from standard input and writes the non-empty ones back to standard output. In most cases it will be used with...
1
by: Ethan Metsger | last post by:
Hi, all. I apologize for what is perhaps a newb question. I'm in the process of transitioning our testing framework from Perl to Python. While that alone probably sets off some red flags,...
12
by: jamik | last post by:
What is the meaning of "flushing the standard output"?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.