2011-06-21 75 views
0

有困難的時候瞭解如何讓Python調用系統函數...蟒蛇系統調用

the_file = ('logs/consolidated.log.gz')   
webstuff = subprocess.Popen(['/usr/bin/zgrep', '/meatsauce/', the_file ],stdout=subprocess.PIPE) % dpt_search 
    for line in webstuff.stdout: 
     print line 

試圖讓蟒蛇建立與我的搜索字符串的另一個文件。

謝謝!

+0

我不知道你打算如何寫你的工作... –

+0

:)在Perl中,你會寫系統或qx(「/ usr/bin/zgrep等等filename> output「); – Cmag

+1

你有一個'%'在那裏。這不是'%'*可能*去的地方,更不用說它會去的地方。 –

回答

1

我推薦道格·赫爾曼誰(的引用)PyMotW Subprocess page「讀取文檔,所以你不必」

除了:

f = file('sourcefile') 
for line in f: 
    if 'pattern' in line: 
      # mind the , at the end, 
      # since there's no stripping involved 
      # and print adds a newline without it 
      print line, 
如果需要分開匹配的正則表達式

Python Standard Library documentation for the re module的文檔也參考PyMotW Regular Expression page

+0

您可能正在閱讀gzipped文件: http://docs.python.org/library/gzip – serverhorror