5

I'm trying to get the output of a adb command using the following code:

pathCmd = './adb shell pm path ' + packageName


pathData = subprocess.Popen(pathCmd,stdout = subprocess.PIPE)
result = pathData.stdout.read()
print result

Any idea why doesn't this command work?

This is the error I see:

OSError: [Errno 2] No such file or directory

I can get the output as os.system but it fails for a subprocess

1
  • 1
    Read the subprocess' docs. Pass the command as a list. Use check_output(). os.system() can't possibly get you output, it only returns exit status. Commented Dec 24, 2013 at 2:43

4 Answers 4

1
import subprocess

ADB_PATH="adb"

def adbdevices(adbpath=ADB_PATH):
    return set([device.split('\t')[0] for device in subprocess.check_output([adbpath, 'devices']).splitlines() if device.endswith('\tdevice')])

def adbshell(command, serial=None, adbpath=ADB_PATH):
    args = [adbpath]
    if serial is not None:
        args.extend(['-s', serial])
    args.extend(['shell', command])
    return subprocess.check_output(args)

def pmpath(pname, serial=None, adbpath=ADB_PATH):
    return adbshell('pm path {}'.format(pname), serial=serial, adbpath=adbpath)
Sign up to request clarification or add additional context in comments.

1 Comment

if pname may contain a space in it then you could use pipes.quote(pname) to escape it for shell (assuming /bin/sh-like syntax): 'pm path {}'.format(pipes.quote(pname)) if command should be a string. Otherwise, pass command as a list: args.extend(['shell'] + command).
1

I tried this in python and it worked for me. This will get the correct version value even if it is in float.

import subprocess

def get_android_os():
    get_os_version = subprocess.check_output("adb shell getprop ro.build.version.release", shell=True)
    os_version = get_os_version.decode("utf-8")
    os_version = str(os_version)
    os_version = os_version.strip()

Comments

0

You should use check_output, below is my code which works successfully.

from subprocess import check_output, CalledProcessError

from tempfile import TemporaryFile

def __getout(*args):
    with TemporaryFile() as t:
        try:
            out = check_output(args, stderr=t)
            return  0, out
        except CalledProcessError as e:
            t.seek(0)
            return e.returncode, t.read()

# cmd is string, split with blank
def getout(cmd):
    cmd = str(cmd)
    args = cmd.split(' ')
    return __getout(*args)

def bytes2str(bytes):
    return str(bytes, encoding='utf-8')

def isAdbConnected():
    cmd = 'adb devices'
    (code, out) = getout(cmd)
    if code != 0:
        print('something is error')
        return False
    outstr = bytes2str(out)
    if outstr == 'List of devices attached\n\n':
        print('no devices')
        return False
    else:
        print('have devices')
        return True

Call isAdbConnected() to check whether device is connected. Hope to help you.

Comments

0
from subprocess import Popen, PIPE

with Popen(['adb devices'], shell=True,stdout=PIPE) as proc:
    for val in proc.stdout.readlines()[1:-1]:
        print(val.decode('UTF-8').replace('device', '').strip())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.