为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

米脂 2021-06-11 14:05:59
我的工作termux,的android 8.0。我正在使用以下实现getchar:class _Getch:    """Gets a single character from standard input.  Does not echo to thescreen."""    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 包引发了同样的错误)
查看完整描述

1 回答

?
天涯尽头无女友

TA贡献1831条经验 获得超9个赞

嗯,这是我的解决方案。


class _GetchUnix:

    def __init__(self):

        import tty, sys


    def __call__(self):

        import subprocess,sys

        t=subprocess.check_output(['bash','-c','read -s -n1 ans; echo $ans'],stdin=sys.stdin)

        return chr(t[0])


查看完整回答
反对 回复 2021-06-15
  • 1 回答
  • 0 关注
  • 227 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号