2012-01-03 21 views
4

でsubprocess.callからの出力をキャッチすると、だから私のsubprocess.callからの出力を保存しようとしているイムが、私は次のエラーを取得しておいてください。次の AttributeError: 'int' object has no attribute 'communicate'Pythonの:標準出力

コードされる:

p2 = subprocess.call(['./test.out', 'new_file.mfj', 'delete1.out'], stdout = PIPE) 
output = p2.communicate[0] 

答えて

4

あなたはcall()の代わりにsubprocess.Popen()を探しています。

また、p2.communicate()[0]に変更する必要があります。

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 
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 
関連する問題