Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Backport of subprocess.check_output() from Python 2.7 to Python 2.6

View gist:1027906
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
import subprocess
 
def check_output(*popenargs, **kwargs):
r"""Run command with arguments and return its output as a byte string.
Backported from Python 2.7 as it's implemented as pure python on stdlib.
>>> check_output(['/usr/bin/python', '--version'])
Python 2.6.2
"""
process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs)
output, unused_err = process.communicate()
retcode = process.poll()
if retcode:
cmd = kwargs.get("args")
if cmd is None:
cmd = popenargs[0]
error = subprocess.CalledProcessError(retcode, cmd)
error.output = output
raise error
return output
rcoup commented
raise subprocess.CalledProcessError(retcode, cmd, output=output)

CalledProcessError doesn't take kwargs in earlier versions of Python...

Owner

@rcoup Thanks for the report. Updated the gist so the output is set the right way.

Good job, thanks!

thank you for this.

thanks! That was helpful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.