简体   繁体   中英

python3 subprocess delivers no output

I have the following problem. In a groovy prog I start a python program that starts a subprocess. The output of this subprocess will be read into a variable. (os is ubuntu)

git = subprocess.Popen(args, stdout = subprocess.PIPE, env=environ)
data = git.stdout.read(); 

But the variable is always empty and I have no idea why. (Starting the Python within a shell it works)

Almost certainly the output is going out stderr which you are not capturing, you would need to add a stderr = subprocess.PIPE and use communicate but if you are using python >= 2.7 then check_output would be the best to use, any non-zero exit status would raise a CalledProcessError :

from subprocess import check_output,CalledProcessError
try:
   data = check_output(args, env=environ)
except CalledProcessError as e:
    print(e.output)
else:
    print(data)

Try this :

git = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=environ)
data, err = git.communicate()
print(data.decode('utf-8'))
print(err.decode('utf-8'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM