2011-02-14 45 views
2

我是一名Python新手,編寫需要執行大量外部應用程序的Python(2.7)腳本,其中一個應用程序將大量輸出寫入其stderr流。我試圖弄清楚的是一個簡潔而簡潔的方式(用Python)從該子進程的stderr輸出流中獲取最後N行。如何獲取子進程stderr流輸出的最後N行?

目前,我從我的Python腳本運行的外部應用程序,像這樣:

p = subprocess.Popen('/path/to/external-app.sh', stdout=subprocess.PIPE, stderr=subprocess.PIPE) 
stdout, stderr = p.communicate() 

if p.returncode != 0: 
    print "ERROR: External app did not complete successfully (error code is " + str(p.returncode) + ")" 
    print "Error/failure details: ", stderr 
    status = False 
else: 
    status = True 

我想從它的標準錯誤流捕獲輸出的最後N行,使他們能夠被寫入日誌文件或電子郵件等

回答

3
N = 3 # for 3 lines of output 
p = subprocess.Popen(['/path/to/external-app.sh'], 
    stdout=subprocess.PIPE, stderr=subprocess.PIPE) 
stdout, stderr = p.communicate() 

if p.returncode != 0: 
    print ("ERROR: External app did not complete successfully " 
      "(error code is %s)" % p.returncode) 
    print "Error/failure details: ", '\n'.join(stderr.splitlines()[-N:]) 
    status = False 
else: 
    status = True 
+0

謝謝喲你 - 這個工作很好!一個跟進問題:如果我對匹配(即包含)特定字符串(例如「錯誤」)的最後N行感興趣,您將如何修改您的答案? – ssahmed555 2011-02-14 18:08:27

0

如果整個輸出不能存儲在RAM中,然後:

import sys 

from collections import deque 
from subprocess import Popen, PIPE 
from threading import Thread 

ON_POSIX = 'posix' in sys.builtin_module_names 

def start_thread(func, *args): 
    t = Thread(target=func, args=args) 
    t.daemon = True 
    t.start() 
    return t 

def consume(infile, output): 
    for line in iter(infile.readline, ''): 
     output(line) 
    infile.close() 

p = Popen(['cat', sys.argv[1]], stdout=PIPE, stderr=PIPE, 
      bufsize=1, close_fds=ON_POSIX) 

# preserve last N lines of stdout, print stderr immediately 
N = 100 
queue = deque(maxlen=N) 
threads = [start_thread(consume, *args) 
      for args in (p.stdout, queue.append), (p.stderr, sys.stdout.write)] 
for t in threads: t.join() # wait for IO completion 

print ''.join(queue), # print last N lines 
retcode = p.wait() 
+0

現在,從我的Python腳本運行的外部應用程序不會生成大量的輸出,因此我認爲我很安全。但是當我遇到一個產生太多輸出的應用程序時,你的代碼看起來非常有用。謝謝。 – ssahmed555 2011-02-14 18:10:32

相關問題