3

Pythonを介してADBコマンドを実行していますが、ある程度は正常に機能します。コードは:

#!/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]

私が得る出力は次のとおりです:

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

私がそれぞれをリストにして、その中に各要素を保存することができるどんな提案でも。リストは次のようになります

list = [cache、sdcard、acct、mnt、vendor..]など。

4

1 に答える 1

6

check_outputコンビニエンス機能を使ってみませんか?

#!/usr/bin/env python
import subprocess

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

ここでは問題なく動作します(Ubuntuボックス)。改行区切り文字は「\n」ではなく「\r\n」であることに注意してください。

于 2012-08-19T10:00:25.167 に答える