Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Can';无法摆脱不受支持的操作:文件否_Python_Python 2.7 - Fatal编程技术网

Python Can';无法摆脱不受支持的操作:文件否

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 以下错误始

要从键盘读取一个字符,我使用以下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
以下错误始终出现:

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编程的新手。我从命令行运行,谢谢你的提示