簡體   English   中英

Python和Android Fastboot的輸出

[英]Output from Python and Android Fastboot

我正在創建一個Python腳本以使用一些fastboot命令,並且我正在嘗試做

fastboot getvar product

為了看看我選擇了什么產品。 問題是當我運行以下代碼時:

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

出是空的。 如果我通過設備而不是全部getvar,它將很好地工作。

我認為這與這個堆棧溢出問題有關,但是我很難將其翻譯成python:

批處理文件中的fastboot getvar

我怎樣才能將getvar的輸出返回一個字符串,而不是僅僅輸出到終端?

編輯:

我找到了一個為adb做類似功能的人的github帳戶,並對其進行了修改以實現我想要的功能:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM