os.system
這個命令只能簡單粗暴的執(zhí)行命令,想要,是不能獲取到輸出的。
os.popen
這個和打開文件有點像,執(zhí)行命令,然后讀出來。
例子
比如我這里連接了三個設備:
List of devices attached
3cdbd05d7cf5 device
3f33d62a7cf5 device
8212971d7ce5 device
現(xiàn)在要讀取所有已經(jīng)連接的android設備:
import re
import os
f = os.popen('adb devices', 'r')
s = f.read()
re.findall('(\w+)\s*device\s+', s)
# ['3cdbd05d7cf5', '3f33d62a7cf5', '8212971d7ce5']