Skip to content

Instantly share code, notes, and snippets.

@papamoose
Last active August 29, 2015 14:02
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 papamoose/0417fb6ccffd1682a6f6 to your computer and use it in GitHub Desktop.
Save papamoose/0417fb6ccffd1682a6f6 to your computer and use it in GitHub Desktop.
python subprocess how to
# http://www.bogotobogo.com/python/python_subprocess_module.php
#!/usr/bin/python
import subprocess
# Example 1
out = subprocess.Popen(['df', '-h'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
(stdout, stderr) = out.communicate()
exitcode = out.returncode
print stdout
print stderr
print exitcode
----
# Example 2
(stdout, stderr, exitcode) = command(['df', '-h'])
def command(args):
out = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False)
(stdout, stderr) = out.communicate()
return stdout, stderr, out.returncode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment