从 python 子进程获取 adb 命令的控制台输出

问题描述 投票:0回答:1
import subprocess

while True:
    subprocess.call(input("> "), shell=True)

比方说,当我运行

adb shell settings get system volume_music_speaker
时,如何获取控制台输出并存储它,也就是说,它是最大音量,因此返回值为14。我已经尝试过

while True:
    a = subprocess.call(input("> "), shell=True)
    print(a)

但我总是得到 0。谢谢。

python python-3.x terminal adb
1个回答
1
投票

https://docs.python.org/3/library/subprocess.html#subprocess.PIPE

with subprocess.Popen(input("> "),
    shell=True,
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE,
) as your_process:
    output, err = your_process.communicate()
© www.soinside.com 2019 - 2024. All rights reserved.