【问题标题】:How to get the latest result from an adb logical grep search from python?如何从 python 的 adb 逻辑 grep 搜索中获取最新结果?
【发布时间】:2016-05-16 18:21:50
【问题描述】:

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

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)

【问题讨论】:

标签: python subprocess adb logcat android-logcat


【解决方案1】:

从子进程的标准输出中获取最后一行:

#!/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?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-24
    • 2021-09-30
    • 1970-01-01
    • 2012-04-10
    • 2020-06-27
    • 1970-01-01
    • 2020-09-28
    • 2019-06-14
    相关资源
    最近更新 更多