python进入adb shell交互模式

import subprocess

#方法一:进入某个环境执行语句(adb shell),注意shell内部命令需要带
,执行完后一定记得执行exit命令退出,否则会阻塞
obj = subprocess.Popen(['adb', 'shell'], shell = True, stdin=subprocess.PIPE, stdout=subprocess.PIPE ,stderr=subprocess.PIPE)
obj.stdin.write('ls
'.encode('utf-8'))
obj.stdin.write('exit
'.encode('utf-8'))  #重点,一定要执行exit
info,err = obj.communicate()
print(info.decode('gbk'))
print(err.decode('gbk'))

#方法二:进入某个环境执行语句(adb shell),命令用列表方式全部列出
cmds = [
    "cd data",
    'cd data',
    "ls",
    "exit",#这是是非常关键的,退出
]
obj = subprocess.Popen("adb shell", shell= True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
info = obj.communicate(("
".join(cmds) + "
").encode('utf-8'));
for item in info:
    if item:
        print(item.decode('gbk'))

  

多思考也是一种努力,做出正确的分析和选择,因为我们的时间和精力都有限,所以把时间花在更有价值的地方。
原文地址:https://www.cnblogs.com/LiuXinyu12378/p/12519280.html