python中的getchar返回权限被拒绝(andorid 8.0)

我的工作termux,的android 8.0。


我正在使用以下实现getchar:


class _Getch:

    """Gets a single character from standard input.  Does not echo to the

screen."""

    def __init__(self):

        try:

            self.impl = _GetchWindows()

        except ImportError:

            self.impl = _GetchUnix()


    def __call__(self): return self.impl()



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)

对tcsetattr的调用返回权限被拒绝。security feature我猜是新的。


Traceback (most recent call last):

  File "piano.py", line 103, in <module>

    char=getch()

  File "piano.py", line 21, in __call__

    def __call__(self): return self.impl()

  File "piano.py", line 33, in __call__

    tty.setraw(sys.stdin.fileno())

  File "/data/data/com.termux/files/usr/lib/python3.6/tty.py", line 28, in setraw

    tcsetattr(fd, when, mode)

termios.error: (13, 'Permission denied')

如何克服这一点?( readchar 包引发了同样的错误)


米脂
浏览 228回答 1
1回答

天涯尽头无女友

嗯,这是我的解决方案。class _GetchUnix:&nbsp; &nbsp; def __init__(self):&nbsp; &nbsp; &nbsp; &nbsp; import tty, sys&nbsp; &nbsp; def __call__(self):&nbsp; &nbsp; &nbsp; &nbsp; import subprocess,sys&nbsp; &nbsp; &nbsp; &nbsp; t=subprocess.check_output(['bash','-c','read -s -n1 ans; echo $ans'],stdin=sys.stdin)&nbsp; &nbsp; &nbsp; &nbsp; return chr(t[0])
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python