Last active
August 29, 2015 14:02
-
-
Save papamoose/0417fb6ccffd1682a6f6 to your computer and use it in GitHub Desktop.
python subprocess how to
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
# 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