所以我试图保存subprocess.call
的输出但是我一直收到以下错误:
AttributeError: 'int' object has no attribute 'communicate'
代码如下:
p2 = subprocess.call(['./test.out', 'new_file.mfj', 'delete1.out'], stdout = PIPE)
output = p2.communicate[0]
答案 0 :(得分:4)
您正在寻找subprocess.Popen()
而不是call()
。
您还需要将其更改为p2.communicate()[0]
。
答案 1 :(得分:3)
那是因为subprocess.call
返回一个int:
subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False)
Run the command described by args. Wait for command to complete, then return the returncode attribute.
看起来你想要subprocess.Popen().
这是我必须执行此操作的典型代码:
p = Popen(cmd, stdout=PIPE, stderr=PIPE, bufsize=256*1024*1024)
output, errors = p.communicate()
if p.returncode:
raise Exception(errors)
else:
# Print stdout from cmd call
print output
答案 2 :(得分:0)
您应该使用子流程
try:
subprocess.check_output(['./test.out', 'new_file.mfj', 'delete1.out'], shell=True, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as exception:
print exception.output