Skip to content

Instantly share code, notes, and snippets.

@mminer
Created November 20, 2014 22:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mminer/3a49476c3a48fea18d64 to your computer and use it in GitHub Desktop.
Save mminer/3a49476c3a48fea18d64 to your computer and use it in GitHub Desktop.
Python function that runs a shell command and returns output as string.
from subprocess import Popen, PIPE
def cmd(*args):
"""Runs a shell command and returns the output as a string."""
out, err = Popen(args, stdout=PIPE, stderr=PIPE).communicate()
# Convert output from byte array to string.
out = out.decode('utf-8')
err = err.decode('utf-8')
if err:
raise Exception(err)
return out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment