如果我在命令下运行,那么python将返回出色的结果。result_aftermatch= subp.Popen('ls -lrt', stdout=subp.PIPE,stderr=subp.PIPE,shell=True)
但是以同样的方式,我需要用代码从文件中提取行如下所示...
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
请帮忙。
最佳答案
问题是您将命令作为多个参数传递。您需要将它们作为列表或元组传递。
喜欢:
subp.Popen([ 'egrep','list_of_IDs','/home/bimlesh/python/result.log' ], stdout=subp.PIPE,stderr=subp.PIPE,shell=True)
关于python - 在python的子进程Popen()中执行cat命令,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17030961/