Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python subprocess.call and subprocess.Popen giving me different outputs

When using subprocess.call, the output is what is expected.

result = subprocess.call([securefx, '/NoPrompt', '/Q', '/RetryCount', retries, 
              '/RetryDelay', '1', '/Log', sfxLogFile, '/List', '/S', session])

Printing the result will output something like -533428 or 0

But when I run

args = [securefx, '/NoPrompt', '/Q', '/RetryCount', retries, '/RetryDelay', '1', 
       '/Log', sfxLogFile, '/List', '/S', session]
process = subprocess.Popen(args, stdout=subprocess.PIPE)
result = process.communicate()[0]

and print the result, I get blank or otherwise stderr = "None".

Why are they different? The reason I'm trying Popen even though call is working is because of this: Python subprocess.call on sfxcl.exe not working from Windows 2003 Task Scheduler <- thought I'd give it a go...

like image 207
kouri Avatar asked Dec 22 '22 10:12

kouri


1 Answers

subprocess.Popen returns a Popen object, which you can use to communicate with the process and get output, however subprocess.call will only return the return code of the process:

subprocess.call(*popenargs, **kwargs)
  Run command with arguments. Wait for command to complete, then return the returncode attribute.

So subprocess.call is basically equivalent to the following code, and only exists for convenience:

def call(*popenargs, **kwargs):
    p = subprocess.Popen(*popenargs, **kwargs)
    return p.wait()
like image 102
Andrew Clark Avatar answered Dec 24 '22 01:12

Andrew Clark