Linux:python:在raw_input()之前清除输入缓冲区 [英] Linux : python : clear input buffer before raw_input()

查看:428
本文介绍了Linux:python:在raw_input()之前清除输入缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过一些有关此问题的话题,但这似乎并不能解决我的问题. 我正在运行linux,当我使用raw_input()时,每个之间都有一个暂停,它将获取我之前按下的数据,这是一个示例:

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 the data that I have pressed before, here is an example :

 import time
 a = raw_input("first input")
 b = raw_input("second input")
 time.sleep(5)
 #flush junk?
 a = raw_input("third input")
 b = raw_input("fourth input")

如果我在5秒钟内按任意键,然后按Enter,则另外两个原始输入将采用该输入.我希望能够刷新数据并提示用户.

if I press any keys followed by the enter during the 5 seconds, the two other raw input will take the input. I would like to be able to flush the data and let the user be prompted.

谢谢.

推荐答案

对于Unix,您可以使用 termios.tcflush

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)

丢弃文件描述符fd上的排队数据.队列选择器指定哪个队列:输入队列为TCIFLUSH,输出队列为TCOFLUSH,两个队列均为TCIOFLUSH.

这篇关于Linux:python:在raw_input()之前清除输入缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆