Question

allocating I want to launch a process and retrieve the stdout and stderr. I don t really care about getting this in real time.

I wanted to use subprocess.check_ouput(), but the process might fail. After reading StackOverflow and the Python docs I added a try .. catch block:

def execute(cmd,timeinsec=60):
  print("execute ",cmd, " with time out ",timeinsec)
  try:
    output = subprocess.check_output(cmd, stderr=subprocess.STDOUT, timeout=timeinsec,universal_newlines=True)
  except subprocess.TimeoutExpired:
    print("timeout expired")
    return "",2
  except subprocess.CalledProcessError:
    print("called process failed")
    return "",1
  print ('The command returned the following back to python:'+output)
  return output,0

But when I print the output with output.decode('utf-8') I just get the first line of the output.

Note : I'm running this in the MSys environment distributed with Msysgit 1.8 on windows.

Do you have any idea of what can be wrong? Do you know any better way to do this?

Was it helpful?

Solution 2

Solution

The problem was that the application in windows launched in the subprocess was allocating a console (built using Visual) and the stdout of the process was already redirected outside, and only one print was done in the original cout before this redirection

OTHER TIPS

You must be using . Please tag your question accordingly. Also, I am not sure why you are calling read() method of output. The output is a byte string and does not have a read() method. The following code works for me:

#! /usr/bin/env python3
import subprocess

try :
    retcode = 0
    cmd = ["/bin/ls", "/usr/local"]
    output = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
    output = e.output
    retcode = e.returncode

print(output.decode('utf-8'))
print(retcode)

The output is:

bin
etc
games
include
lib
man
sbin
share
src

0

If I trigger an error by replacing /usr/local with /usr/localfoo (which does not exist), then the output is:

/bin/ls: cannot access /usr/localfoo: No such file or directory

2

Finally, you can add universal_newlines=True to check_output() call and not have to worry about calling decode() on the output:

...
output = subprocess.check_output(cmd, stderr=subprocess.STDOUT,universal_newlines=True)
...
...
print(output)

Please take the above example and see if you can make it reproduce your problem. If you can reproduce the problem, please post your code, its output, and all error messages (copy, paste, and reformat for SO).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top