re Module 正则表达式
example
执行adb devices 命令,获取当前连接到主机的所有手机型号
执行结果如下:
Step 1 : adb device
cmd result =
####################################
List of devices attached
HEE6R15617005387 device
####################################
['HEE6R15617005387']
#-*- coding:utf-8 -*-
'''
Created on 2017年6月13日
确定有几部手机连接在主机上
@author: will
'''
import os, re, logging
def findDevices():
cmd = 'adb devices'
print 'Step 1 : adb device \n'
result = os.popen(cmd)
dis = result.read()
print 'cmd result = \n' + '####################################\n\n' + dis + '####################################\n'
Devices = re.findall(r'(.*?)\tdevice', dis)
if len(Devices) >= 1:
DeviceIds = Devices[0:]
print(u'共找到%s个手机'%str(len(Devices)))
for i in DeviceIds:
print(u'ID 为%s'%i)
return DeviceIds
else:
print(u'没找到手机,请检查USB连接')
return
print Devices
if __name__ == '__main__':
findDevices()
pass
执行结果如下:
Step 1 : adb device
cmd result =
####################################
List of devices attached
e802c426 device
HEE6R15617005387 device
####################################
共找到2个手机
ID 为e802c426
ID 为HEE6R15617005387