Created
November 20, 2014 22:50
-
-
Save mminer/3a49476c3a48fea18d64 to your computer and use it in GitHub Desktop.
Python function that runs a shell command and returns output as string.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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