2012-01-03 245 views

回答

4

您正在尋找subprocess.Popen()而不是call()。您還需要將其更改爲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