I’m writing a simple alarm utility in Python.
#!/usr/bin/python
import time
import subprocess
import sys
alarm1 = int(raw_input("How many minutes (alarm1)? "))
while (1):
time.sleep(60*alarm1)
print "Alarm1"
sys.stdout.flush()
doit = raw_input("Continue (Y/N)?[Y]: ")
print "Input",doit
if doit == 'N' or doit=='n':
print "Exiting....."
break
I want to flush or discard all the key strokes that were entered while the script was sleeping and only accept the key strokes after the raw_input() is executed.
I’m running this on Windows XP.
It would help to know what operating system you’re using, as this is a very operating-system-specific question. For example, Kylar’s answer doesn’t work on Windows because sys.stdin doesn’t have a fileno attribute.
I was curious and threw together a solution using curses, but this won’t work on Windows either:
EDIT: ah, Windows. Then you can use the msvcrt module. Note that the code below isn’t perfect, and it doesn’t work in IDLE at all: