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')
發佈了104 篇原創文章 · 獲贊 68 · 訪問量 39萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章