Python STDIN用户输入问题 [英] Python STDIN User Input Issue

查看:107
本文介绍了Python STDIN用户输入问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

在执行这两个功能时与STDIN冲突.是什么会导致第二个功能无法正确读取STDIN?如果未执行第一个功能,则第二个功能在读取输入时没有问题.

Conflict with STDIN when executing these two functions. What would cause the second function to not read STDIN correctly? If the first function is not executed, the second function has no issue reading the input.

要清除STDIN缓冲区,我已经尝试过:

To clear STDIN buffer I've tried:

'sys.stdin.flush'

'tcflush(sys.stdin, TCIOFLUSH)'

Python代码

import sys, select

def stdin_read():

    print "[stdin read] You must answer Y/N and press ENTER"
    sys.stdout.flush()
    response = sys.stdin.read(1)
    print "You said '{}'".format(response)


def stdin_timeout():

    print "[stdin timeout] You must answer YES / NO in 10 seconds and press ENTER"
    sys.stdout.flush()

    sys.stdin.flush()

    i, o, e = select.select( [sys.stdin], [], [], 10 )

    if (i):
        print "You said '{}'".format(sys.stdin.readline().strip())
        exit(0)
    else:
        print "You said nothing!"
        exit(1)


stdin_read()
stdin_timeout()

Python输出两个函数:

:~# python input.py
[stdin read] You must answer Y/N and press ENTER
n
You said 'n'
[stdin timeout] You must answer YES / NO in 10 seconds and press ENTER
no
You said ''
:~# no
-bash: no: command not found

Python输出第二个功能

~# python input.py
[stdin timeout] You must answer YES / NO in 10 seconds and press ENTER
no
You said 'no'

推荐答案

问题来自 unix中stdin刷新的行为

我会通过注释您的代码来回答:

I'll answer by commmenting your code:

import sys, select

def stdin_read():

    print "[stdin read] You must answer Y/N and press ENTER"
    # sys.stdout.flush() # not needed
    # you are reading one byte here, but the user enters at least two bytes
    # (e.g 'y' + LF(enter key)) so the LF byte (and possibly additional bytes)
    # remain in the stdin buffer.
    response = sys.stdin.read(1) # (->LF REMAINS)
    print "You said '{}'".format(response)


def stdin_timeout():

    print "[stdin timeout] You must answer YES / NO in 10 seconds and press ENTER"
    #sys.stdout.flush() #  not needed
    # the behaviour of stdin flushing in unix is not defined (see 
    # link above)
    sys.stdin.flush()
    # thus the LF still remains in the stdin buffer

    # the select call blocks until the file discriptor is 'ready' for
    # the corresponding i/o operation. DISCLAIMER: assumptions are
    # following.... By default python does a open call which uses
    # buffered reads, so even if you did a read(1) it will likely read
    # more data from stdin and the select call blocks until the next LF
    # arrives.
    i, o, e = select.select( [sys.stdin], [], [], 10 )

    if (i):
        # you are just reading the left over bytes from above (->LF REMAINS)
        print "You said '{}'".format(sys.stdin.readline().strip())
        # Proof: readline() again:
        print "You missed '{}'".format(sys.stdin.readline().strip())
        exit(0)
    else:
        print "You said nothing!"
        exit(1)


stdin_read()
stdin_timeout()

但是也许最好使用另一种方法在Python中使用超时键盘输入

But maybe it would be better to use another approach Keyboard input with timeout in Python

这篇关于Python STDIN用户输入问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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