Skip to content

Instantly share code, notes, and snippets.

@gregelin
Created October 31, 2015 10:27
Show Gist options
  • Save gregelin/ee495c56aa71eb4d4f3a to your computer and use it in GitHub Desktop.
Save gregelin/ee495c56aa71eb4d4f3a to your computer and use it in GitHub Desktop.
Python return (status, output) of executing cmd in a shell.
def getstatusoutput(cmd):
"""Return (status, output) of executing cmd in a shell."""
"""This new implementation should work on all platforms."""
import subprocess
pipe = subprocess.Popen(cmd, shell=True, universal_newlines=True,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output = str.join("", pipe.stdout.readlines())
sts = pipe.wait()
if sts is None:
sts = 0
return sts, output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment