阅读Python中的单个字符(残培风格)是不是在unix工作
题
任何时间我使用在 http://code.activestate.com/recipes/134892配方/ 我似乎无法得到它的工作。它总是引发以下错误:
Traceback (most recent call last):
...
old_settings = termios.tcgetattr(fd)
termios.error: (22, 'Invalid argument)
我最好的想法是,那是因为我运行它在Eclipse这样termios
抛出一个合适的有关文件的描述符。
解决方案
此是工作在Ubuntu 8.04.1,Python的2.5.2,我得到没有这样的错误。可能是你应该尝试的命令行,偏食可以使用自己的标准输入,我得到完全相同的错误,如果我从永IDE运行它,而是从命令行它的伟大工程。 原因是IDE e.g永用自己的有类netserver.CDbgInputStream为sys.stdin 所以sys.stdin.fileno是零,这就是为什么错误。 基本上IDE标准输入是不是tty(印刷sys.stdin.isatty()为假)
class _GetchUnix:
def __init__(self):
import tty, sys
def __call__(self):
import sys, tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
getch = _GetchUnix()
print getch()
其他提示
把端子插入原始模式并不总是一个好主意。其实这足以清除ICANON位。下面是超时支持的getch()的另一种形式:
import tty, sys, termios
import select
def setup_term(fd, when=termios.TCSAFLUSH):
mode = termios.tcgetattr(fd)
mode[tty.LFLAG] = mode[tty.LFLAG] & ~(termios.ECHO | termios.ICANON)
termios.tcsetattr(fd, when, mode)
def getch(timeout=None):
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
setup_term(fd)
try:
rw, wl, xl = select.select([fd], [], [], timeout)
except select.error:
return
if rw:
return sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
if __name__ == "__main__":
print getch()
不隶属于 StackOverflow