Skip to content

Instantly share code, notes, and snippets.

@terryoy
Created May 15, 2014 04:51
Show Gist options
  • Save terryoy/c6009bbf084eb5fcfedc to your computer and use it in GitHub Desktop.
Save terryoy/c6009bbf084eb5fcfedc to your computer and use it in GitHub Desktop.
Python calling OS command line
import subprocess
def run_program(cmdargs):
"""Execute an OS program with arguments(in list form), and return the (returncode, stdout, stderr) result"""
p = subprocess.Popen(cmdargs, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output = p.communicate()
return p.returncode, output[0], output[1]
# example;
# run_program(['ls', '-l'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment