22

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.

3
  • 2
    you don't need that semicolon after sys.stdout.flush(); Mar 26, 2010 at 4:21
  • My bad, you are right, I don't need the semi-colon. Fixed the code.
    – Amjith
    Mar 26, 2010 at 5:20
  • 2
    "flush or discard all the key strokes"? Do you mean "Read and Ignore" You can't easily "flush" input. Why not do the simple, obvious thing: read them and ignore them?
    – S.Lott
    Mar 26, 2010 at 10:15

5 Answers 5

16

From Rosetta Code

def flush_input():
    try:
        import msvcrt
        while msvcrt.kbhit():
            msvcrt.getch()
    except ImportError:
        import sys, termios    #for linux/unix
        termios.tcflush(sys.stdin, termios.TCIOFLUSH)

The try part is for Windows platform. I have not personally tested this part. But the except section works on linux terminal. termios module has some terminal interface functions. the tcflush can flush input or output buffered data. This part definitely works in my tests.

4
  • 1
    Why should the OP trust your answer? A good answer will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO.
    – B001ᛦ
    Aug 17, 2016 at 10:08
  • 2
    @bub, thanks for the hint. I didn't think there is much explantation needed on this one. will edit my answer
    – kollery
    Aug 17, 2016 at 10:19
  • This is similar to what @Callahad had. Just wraps in a nice 'def'
    – kollery
    Aug 17, 2016 at 10:25
  • termios.tcflush(sys.stdin, termios.TCIOFLUSH) good idea Mar 1 at 16:32
14

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:

#!/usr/bin/python                                                               

import time
import sys
import curses

def alarmloop(stdscr):
    stdscr.addstr("How many seconds (alarm1)? ")
    curses.echo()
    alarm1 = int(stdscr.getstr())
    while (1):
        time.sleep(alarm1)
        curses.flushinp()
        stdscr.clear()
        stdscr.addstr("Alarm1\n")
        stdscr.addstr("Continue (Y/N)?[Y]:")
        doit = stdscr.getch()
        stdscr.addstr("\n")
        stdscr.addstr("Input "+chr(doit)+"\n")
        stdscr.refresh()
        if doit == ord('N') or doit == ord('n'):
            stdscr.addstr("Exiting.....\n")
            break

curses.wrapper(alarmloop)

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:

#!/usr/bin/python

import time
import subprocess
import sys
import msvcrt

alarm1 = int(raw_input("How many seconds (alarm1)? "))

while (1):
    time.sleep(alarm1)
    print "Alarm1"
    sys.stdout.flush()

    # Try to flush the buffer
    while msvcrt.kbhit():
        msvcrt.getch()

    print "Continue (Y/N)?[Y]"
    doit = msvcrt.getch()
    print "Input",doit
    if doit == 'N' or doit=='n':
        print "Exiting....."
        break
2
  • I'm running this in Windows xp.
    – Amjith
    Mar 26, 2010 at 5:21
  • 1
    Then you might want to try the msvcrt module; see the edit above. Mar 26, 2010 at 5:28
10

On Unices, you can use termios.tcflush():

import time
import subprocess
import sys
from termios import tcflush, TCIOFLUSH

alarm1 = int(raw_input("How many minutes (alarm1)? "))

while (1):
    time.sleep(60*alarm1)
    print "Alarm1"
    sys.stdout.flush();
    tcflush(sys.stdin, TCIOFLUSH)
    doit = raw_input("Continue (Y/N)?[Y]: ")
    print "Input",doit
    if doit == 'N' or doit=='n':
        print "Exiting....."
        break
5
#!/usr/bin/python

import time
import subprocess
import sys
import os, select

alarm1 = int(raw_input("How many minutes (alarm1)? "))

while (1):
    time.sleep(3*alarm1)
    print "Alarm1"
    sys.stdout.flush()
    while select.select([sys.stdin.fileno()], [], [], 0.0)[0]:
        os.read(sys.stdin.fileno(), 4096)
    doit = raw_input("Continue (Y/N)?[Y]: ")
    print "Input",doit
    if doit == 'N' or doit=='n':
        print "Exiting....."
        break
3
  • I reduced your sleep time to 3 seconds, for testing :)
    – Kylar
    Mar 26, 2010 at 3:47
  • This code didn't work on Windows XP. Can you please explain the various parameters you are sending to the select.select()?
    – Amjith
    Mar 26, 2010 at 5:22
  • 4
    Ahh, I didn't realize you were on windows. This won't work at all (I saw the /usr/bin/python and assumed. My bad.) The docs for select are here: docs.python.org/library/select.html but on windows it will only bind to a socket.
    – Kylar
    Mar 26, 2010 at 17:35
0

Solution:

I have used (and worked in my case):

import sys

...
sys.stdin.flush()
...

Why this answer?

I have added this answer to this thread because I got here with the question "What was the syntax for flushing stdin?". I read the entries and thought that there had to be some flush method for stdin. I found it. I was surprised that it was not used in any entry and I thought that it should be.

Related documentation

4
  • Hello, please don't just submit code in your answer(s), add some details as to why you think this is the optimal solution.
    – Destroy666
    May 29 at 17:47
  • Thank you for your comment. I really thought that my simple answer was very appropriate for people with the same question of this thread ("How to flush the input stream?"). I actually came to this thread with the same question, read the entries, and thought that there had to be some flush method for stdin. I found it and was surprised that it was not used in any entry. May 31 at 8:31
  • Ok. I meant more like "it's better than other solutions because X, Y, Z", you could also link to the documentation or anything alike.
    – Destroy666
    May 31 at 14:24
  • Thank you for your suggestion. It is certainly a good idea to refer to the relevant documentation. I will try to take this into account in future entries. On the other hand, I don't think this solution is better than the others, but complementary. The others address an aspect commented in the text of the question, while mine simplifies the action of flushing stdin (title of the question). Jun 5 at 14:29

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.