Executing Android commands through Python and storing the result in a list

13,288

Shouldn't you use the check_output convenience function?

#!/usr/bin/env python
import subprocess

cmd = 'adb shell ls'
s = subprocess.check_output(cmd.split())
print s.split('\r\n')

It works just fine here (Ubuntu box). Note that the newline separators are '\r\n' instead of just '\n'.

Share:
13,288
aditya.gupta
Author by

aditya.gupta

Updated on June 08, 2022

Comments

  • aditya.gupta
    aditya.gupta about 2 years

    I am executing ADB commands through Python and it works fine till some extent. The code is :

    #!/usr/bin/python
    import sys
    import string
    import os
    import subprocess
    
    cmd = 'adb shell ls'
    s = subprocess.Popen(cmd.split())
    print "Again"
    t = str(s)
    for me in t.split('\n') :
        print "Something"
        print me[1]
    

    The output i get is :

    static-243:Scripts adityagupta$ ./hellome.py 
    Again
    Something
    s
    static-243:Scripts adityagupta$ config
    cache
    sdcard
    acct
    mnt
    vendor
    d
    etc
    ueventd.rc
    ueventd.goldfish.rc
    system
    sys
    sbin
    proc
    init.rc
    init.goldfish.rc
    init
    default.prop
    data
    root
    dev
    

    Any suggestion i could make each a list and store each element in it. The list should look like

    list = [cache, sdcard, acct, mnt, vendor ..] and so on.