如何从python中获取adb逻辑grep搜索的最新结果?

时间:2016-05-16 18:21:50

标签: python subprocess adb logcat android-logcat

我已经在后台打开了一个android模拟器实例。我想编写一个python脚本,可以使用子进程模块grep logcat获取某个单词,并将该搜索的最新结果(基于时间戳)返回为字符串。这该怎么做?

adb logcat | grep keyword
>> 00:00:01 keyword
>> 00:00:02 keyword
>> 00:00:03 keyword

想要返回行" 00:00:03关键字"

的python脚本
    proc = subprocess.Popen(['adb', 'logcat', '| grep keyword'], stdout=subprocess.PIPE)
    last_result=read_last_result(proc)

1 个答案:

答案 0 :(得分:0)

从子流程中获取最后一行'标准输出:

#!/usr/bin/env python3
from collections import deque
from subprocess import Popen, PIPE

with Popen('adb -d logcat <filter-spec>'.split(), stdout=PIPE) as adb:
    last_line = deque(adb.stdout, maxlen=1).pop() # get last line

请参阅adb logcat options

如果您想从字面上模拟'adb logcat | grep keyword' shell命令,请参阅How do I use subprocess.Popen to connect multiple processes by pipes?