Search code examples
pythonpython-3.xterminaladb

Get console output of adb command from python subprocces


import subprocess

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

let's say when I run adb shell settings get system volume_music_speaker, how can I get the console output and store it, which let's say, it's max volume so the return value would be 14. I have already tried

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

but I always get 0. Thank you.


Solution

  • 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()