1. 程式人生 > >Python execute adb shell command

Python execute adb shell command

用Python寫些平時開發的輔助指令碼還是非常方便的,做為Android開發有碰到用Python執行adb命令的需求,os.system() 可以執行系統命令但是不支援獲取輸出,再者要考慮到讀取中文輸出之類的問題的話最好使用subprocess:

#!/usr/bin/env python
#encoding=utf-8

import subprocess

def sh(command):
    p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    print
p.stdout.read() sh('ls') sh('adb shell mkdir /data/testDir')