繁体   English   中英

在子进程中执行cat命令,python的Popen()

[英]execute cat command in subprocess,Popen() of python

如果我在命令下运行,那么python返回了很好的结果..

result_aftermatch= subp.Popen('ls -lrt', stdout=subp.PIPE,stderr=subp.PIPE,shell=True)

但同样的方式我要求使用代码从文件greping行如下...

list_of_id=[23,34,56,77,88]
result_aftermatch= subp.Popen('egrep','list_of_IDs','/home/bimlesh/python/result.log', stdout=subp.PIPE,stderr=subp.PIPE,shell=True)
result_lines,result_err= result_aftermatch.communicate()
print result_lines

上面的代码给出了如下错误...

Traceback (most recent call last):
  File "test.py", line 144, in <module>
    result_aftermatch= subp.Popen('egrep','list_of_IDs','/home/bimlesh/python/result.log', stdout=subp.PIPE,stderr=subp.PIPE,shell=True)
  File "/usr/lib/python2.6/subprocess.py", line 573, in __init__
    raise TypeError("bufsize must be an integer")
TypeError: bufsize must be an integer

请帮忙。

问题是你将命令作为多个args传递。 您需要将它们作为列表或元组传递。

喜欢:

subp.Popen([ 'egrep','list_of_IDs','/home/bimlesh/python/result.log' ], stdout=subp.PIPE,stderr=subp.PIPE,shell=True)

我猜你正在寻找这个:

list_of_id = [23,34,56,77,88]
ids_regex = '|'.join([str(i) for i in list_of_id])
result_aftermatch = subp.Popen(['egrep', ids_regex, '/home/bimlesh/python/result.log'], stdout=subp.PIPE, stderr=subp.PIPE)
result_lines, result_err = result_aftermatch.communicate()
print result_lines

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM