gpt4 book ai didi

android - Python 和 Android Fastboot 的输出

转载 作者:行者123 更新时间:2023-11-30 03:29:18 24 4
gpt4 key购买 nike

我正在创建一个 python 脚本来处理一些 fastboot 命令,我正在尝试这样做

fastboot getvar product

为了查看我选择了什么产品。问题是当我运行这段代码时:

p = subprocess.Popen(['fastboot', "getvar", "all"])
out, err = p.communicate()
print "We got: " + out

输出是空的。如果我传入设备而不是 getvar all,它工作正常。

我认为它与这个堆栈溢出问题有关,但我很难将它翻译成 python:

fastboot getvar from batch file

我怎样才能从 getvar 获取字符串中的输出,而不是仅仅输出到终端?

编辑:

我找到了一个github账号,有人给adb做了一个类似的功能,修改了一下,实现了我想要的:

def callFastboot(self, command):
command_result = ''
command_text = 'fastboot %s' % command
results = os.popen(command_text, "r")
while 1:
line = results.readline()
if not line: break
command_result += line
return command_result

out = test.callFastboot("getvar product 2>&1")
print "We got: " + out

问题是这使用了旧的 os.popen 方法。所以我的新问题是一样的,但是我如何使用子流程来做到这一点?

最佳答案

对于 fastboot getvar all,您需要捕获 stderr 而不是 stdout:

print subprocess.check_output(['fastboot', 'getvar', 'all'], stderr=subprocess.STDOUT)

关于android - Python 和 Android Fastboot 的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17748473/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com