Linux : python : clear input buffer before raw_input()

后端 未结 2 1619
生来不讨喜
生来不讨喜 2020-12-06 18:54

I have looked at a few thread about this, but it doesn\'t seem to solve my problem. I am running linux and when I use raw_input(), with a pause between each, it will take th

相关标签:
2条回答
  • 2020-12-06 19:25

    Use keypress getch class of tty (linux) and msvcrt (windows) and use sys.stdout.flush() function to flush the buffer

    0 讨论(0)
  • 2020-12-06 19:27

    For unix you can use termios.tcflush

    from termios import tcflush, TCIFLUSH
    import time,sys
    
    a = raw_input("first input ")
    b = raw_input("second input ")
    
    time.sleep(5)
    tcflush(sys.stdin, TCIFLUSH)
    
    a = raw_input("third input ")
    b = raw_input("fourth input ")
    
    ~$ python foo.py 
    first input 1
    second input 2
    33
    33
    third input 3
    fourth input 4
    

    termios.tcflush(fd, queue)

    Discard queued data on file descriptor fd. The queue selector specifies which queue: TCIFLUSH for the input queue, TCOFLUSH for the output queue, or TCIOFLUSH for both queues.

    0 讨论(0)
提交回复
热议问题