Python如何通過(guò)subprocess調(diào)用adb命令詳解
前言
本文主要給大家介紹了關(guān)于使用Python通過(guò)subprocess調(diào)用adb命令,subprocess包主要功能是執(zhí)行外部命令(相對(duì)Python而言)。和shell類(lèi)似。
換言之除了adb命令外,利用subprocess可以執(zhí)行其他的命令,比如ls,cd等等。
subprocess 可參考: https://docs.python.org/2/library/subprocess.html
在電腦上裝好adb工具,配置好adb的環(huán)境變量,先確保shell中可以調(diào)用adb命令。
代碼示例
Python2.7
類(lèi) Adb,封裝了一些adb的方法
import os
import subprocess
class Adb(object):
""" Provides some adb methods """
@staticmethod
def adb_devices():
"""
Do adb devices
:return The first connected device ID
"""
cmd = "adb devices"
c_line = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0]
if c_line.find("List of devices attached") < 0: # adb is not working
return None
return c_line.split("\t")[0].split("\r\n")[-1] # This line may have different format
@staticmethod
def pull_sd_dcim(device, target_dir='E:/files'):
""" Pull DCIM files from device """
print "Pulling files"
des_path = os.path.join(target_dir, device)
if not os.path.exists(des_path):
os.makedirs(des_path)
print des_path
cmd = "adb pull /sdcard/DCIM/ " + des_path
result = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
print result
print "Finish!"
return des_path
@staticmethod
def some_adb_cmd():
p = subprocess.Popen('adb shell cd sdcard&&ls&&cd ../sys&&ls',
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return_code = p.poll()
while return_code is None:
line = p.stdout.readline()
return_code = p.poll()
line = line.strip()
if line:
print line
print "Done"
some_adb_cmd方法執(zhí)行一連串的命令。各個(gè)命令之間用&&連接。
接著是一個(gè)死循環(huán),將執(zhí)行結(jié)果打印出來(lái)。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- python使用ctypes庫(kù)調(diào)用DLL動(dòng)態(tài)鏈接庫(kù)
- Python調(diào)用REST API接口的幾種方式匯總
- 如何使用python的ctypes調(diào)用醫(yī)保中心的dll動(dòng)態(tài)庫(kù)下載醫(yī)保中心的賬單
- python使用ctypes調(diào)用擴(kuò)展模塊的實(shí)例方法
- python中使用ctypes調(diào)用so傳參設(shè)置遇到的問(wèn)題及解決方法
- Python使用ctypes調(diào)用C/C++的方法
- python調(diào)用百度REST API實(shí)現(xiàn)語(yǔ)音識(shí)別
- python調(diào)用百度語(yǔ)音REST API
- Python調(diào)用C語(yǔ)言的方法【基于ctypes模塊】
- Python 調(diào)用 ES、Solr、Phoenix的示例代碼
相關(guān)文章
Python OpenCV中的numpy與圖像類(lèi)型轉(zhuǎn)換操作
這篇文章主要介紹了Python OpenCV中的numpy與圖像類(lèi)型轉(zhuǎn)換操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12
Python?logging日志模塊的概念與實(shí)踐講解
本文通過(guò)具體的代碼示例為大家解釋了如何高效地使用logging模塊進(jìn)行日志記錄,以及如何避免常見(jiàn)的陷阱,希望可以幫助大家更好地掌握這個(gè)強(qiáng)大的工具2023-07-07
Python3.7 dataclass使用指南小結(jié)
本文將帶你走進(jìn)python3.7的新特性dataclass,通過(guò)本文你將學(xué)會(huì)dataclass的使用并避免踏入某些陷阱。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-02-02
pytorch加載自定義網(wǎng)絡(luò)權(quán)重的實(shí)現(xiàn)
今天小編就為大家分享一篇pytorch加載自定義網(wǎng)絡(luò)權(quán)重的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01
python環(huán)境下OPenCV處理視頻流局部區(qū)域像素值
這篇文章主要為大家介紹了python環(huán)境下OPenCV處理視頻流局部區(qū)域像素值的實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11
Django+Uwsgi+Nginx如何實(shí)現(xiàn)生產(chǎn)環(huán)境部署
這篇文章主要介紹了Django+Uwsgi+Nginx如何實(shí)現(xiàn)生產(chǎn)環(huán)境部署,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07

