2016-05-16 5 views
-1

私はすでにアンドロイドエミュレータのインスタンスをバックグラウンドで開いています。私は特定の単語のlogcatをgrepするためにサブプロセスモジュールを使用できるpythonスクリプトを書いて、その検索結果の最新の(タイムスタンプに基づいて)結果を文字列として返したいと思います。これを行う方法?pythonからのadb論理grep検索の最新結果を得るには?

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

サブプロセス標準出力からの最後の行を取得するには行が返されますPythonスクリプト "0時00分03秒キーワード"

proc = subprocess.Popen(['adb', 'logcat', '| grep keyword'], stdout=subprocess.PIPE) 
    last_result=read_last_result(proc) 
+0

参照、文字通り'adb logcat | grep keyword'シェルコマンドをエミュレートしたい場合は?また、公式のPythonサイトには「[Regular Expressions HOWTO](https://docs.python.org/2/howto/regex.html)」という便利な情報があります。 –

答えて

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を参照してください。

あなたは[再](https://docs.python.org/2/library/re.html)モジュールを試してみましたHow do I use subprocess.Popen to connect multiple processes by pipes?

関連する問題