I don't have a lot of familiarity with Python but I do with Ruby. So I will provide analogues for what I want to achieve
In Ruby I would
val = `adb devices`
to get the "raw output" of adb devices stored in val, and
val=system("adb devices")
to get the status code
I want to perform the same tasks in Python. I looked at
from subprocess import call
call(["adb devices"])
but that failed, I don't want to use os.system cause I want to get some proper error handling. how do i make call work and how do i obtain raw output from backticks in Python
Pass the command and arguments as separate elements of a list:
from subprocess import call
return_code = call(["adb", "devices"])
However, this sends the output to stdout and you can't capture it. Instead you can use subprocess.check_ouput()
:
from subprocess import check_output
adb_ouput = check_output(["adb", "devices"])
# stdout of adb in adb_output.
If the return code is non-zero an exception is raised. You should catch it to see what the return code is. No exception is raised for return code 0:
from subprocess import check_output, CalledProcessError
try:
adb_ouput = check_output(["adb", "devices"])
except CalledProcessError as e:
print e.returncode