Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

android

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.

like image 505
aditya.gupta Avatar asked Sep 12 '26 22:09

aditya.gupta


1 Answers

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'.

like image 76
Vicent Avatar answered Sep 14 '26 12:09

Vicent



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!