Python Can';无法摆脱不受支持的操作:文件否
要从键盘读取一个字符,我使用以下python代码Python Can';无法摆脱不受支持的操作:文件否,python,python-2.7,Python,Python 2.7,要从键盘读取一个字符,我使用以下python代码 def getch(): 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 以下错误始
def getch():
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
以下错误始终出现:
Traceback (most recent call last):
File "/home/pi/Adafruit-Motor-Hat-Python-Library/examples/Testje.py", line 36, in <module>
char = getch()
File "/home/pi/Adafruit-Motor-Hat-Python-Library/examples/Testje.py", line 17, in getch
fd = sys.stdin.fileno()
UnsupportedOperation: fileno
回溯(最近一次呼叫最后一次):
文件“/home/pi/Adafruit-Motor-Hat-Python-Library/examples/Testje.py”,第36行,在
char=getch()
文件“/home/pi/Adafruit-Motor-Hat-Python-Library/examples/Testje.py”,第17行,在getch中
fd=sys.stdin.fileno()
不支持操作:fileno
我在互联网上搜索了一个解决方案,但找不到。也许有人能帮我。嗯,很难知道,因为你对自己的工作环境只字不提。但是Python GUI shell(至少空闲)将
sys.stdin
重定向到一个套接字包装器,该包装器没有定义fileno()
方法。如何运行上述代码?感谢您的反应。我正在空闲状态下运行这个程序作为测试。正如你所注意到的,我是Python编程的新手。我从命令行运行,谢谢你的提示