the_file = ('logs/consolidated.log.gz')
webstuff = subprocess.Popen(['/usr/bin/zgrep', '/meatsauce/', the_file ],stdout=subprocess.PIPE)
for line in webstuff.stdout:
print line
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import subprocess
# filename and searchterm
fn, term = 'access_log.gz', 'hello'
p = subprocess.Popen(['/usr/bin/zgrep', term, fn], stdout=subprocess.PIPE)
for line in p.stdout:
print line
import os
the_file = 'logs/consolidated.log.gz'
webstuff = subprocess.Popen(['/usr/bin/zgrep', os.path.join('/meatsauce/', the_file)],stdout=subprocess.PIPE) % dpt_search
for line in webstuff.stdout:
print line
不完全确定您的问题,但下面的代码片段将使用两个参数调用
zgrep
,一个是searchterm,一个是filename-并逐行打印结果(stdout
):在您发布的代码中,字符串插值(
% dpt_search
)不起作用,因为在模符号前面没有纯字符串-事实上,它应该会失败,原因如下:TypeError: "unsupported operand type(s) for %: 'Popen' and 'str'"
问题在于你如何构建你的论点。你现在的样子,你在跑:
/usr/bin/zgrep /meatsauce/ logs/consolidated.log.gz
注意
/meatsauce/
和logs
之间的空格。。。要做我想做的事,使用
os.path.join
a la:相关问题 更多 >
编程相关推荐