Python如何通过subprocess调用adb命令详解
作者:RustFisher 时间:2023-03-30 13:10:19
前言
本文主要给大家介绍了关于使用Python通过subprocess调用adb命令,subprocess包主要功能是执行外部命令(相对Python而言)。和shell类似。
换言之除了adb命令外,利用subprocess可以执行其他的命令,比如ls,cd等等。
subprocess 可参考: https://docs.python.org/2/library/subprocess.html
在电脑上装好adb工具,配置好adb的环境变量,先确保shell中可以调用adb命令。
代码示例
Python2.7
类 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方法执行一连串的命令。各个命令之间用&&连接。
接着是一个死循环,将执行结果打印出来。
来源:http://rustfisher.github.io/2017/07/05/Python_note/Python-adb/
标签:python,adb命令,subprocess
0
投稿
猜你喜欢
getElementsByTagName vs selectNodes效率 及兼容的selectNodes实现
2024-04-22 13:01:07
使用JavaScript构建JSON格式字符串实现步骤
2024-04-18 09:42:12
基于python使用Pillow做动态图在图中生成二维码以及图像处理
2022-12-29 16:33:03
java实现数据库的数据写入到txt的方法
2024-01-28 06:22:16
python使用正则表达式检测密码强度源码分享
2022-06-26 15:54:42
关于Flask 上下文详细介绍
2023-12-10 01:52:32
range 标准化之获取
2023-09-01 05:35:11
javascript实现自动输出文本(打字特效)
2024-02-25 18:25:14
Python‘==‘ 及 ‘is‘相关原理解析
2021-10-11 15:16:59
Pytorch使用shuffle打乱数据的操作
2021-10-03 08:33:31
MySQL如何优化索引
2024-01-25 17:12:15
Python常用模块用法分析
2022-02-05 05:29:43
Vue.js框架实现购物车功能
2024-04-30 10:46:59
CSS3:文字阴影知多少
2009-06-28 15:33:00
Pyinstaller打包Scrapy项目的实现步骤
2022-05-01 18:07:02
如何实现固定长度的自动编号?
2010-06-03 10:08:00
YUI3设计中的激进和妥协
2010-01-17 09:59:00
浅谈vue获得后台数据无法显示到table上面的坑
2024-05-13 09:07:16
asp xmlhttp下载一句话
2013-06-30 06:47:48
IE8 CSS之生成内容
2008-09-09 22:14:00