Last active
February 23, 2016 05:20
-
-
Save c4urself/59165eb32b639736461f to your computer and use it in GitHub Desktop.
Test out subprocess
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
import subprocess | |
# http://stackoverflow.com/questions/18774476/subprocess-call-logger-info-and-error-for-stdout-and-stderr-respectively | |
# http://stackoverflow.com/questions/21953835/run-subprocess-and-print-output-to-logging | |
def my_buddy_popen(): | |
""" | |
Since only stderr is bound, we only get a value for stderr. if this was a good call (exit 0) | |
then out _would_ exist and sent to parent's and _not_ be in `out`. | |
""" | |
proc = subprocess.Popen(['ls', '-al', '/tmp/arst/not/exist'], stderr=subprocess.PIPE) | |
out, err = proc.communicate() | |
print(out) # None | |
print('hallo') | |
print(err) | |
def tryexc1(): | |
""" | |
Since stderr hasn't been bound, this uses parent's stderr | |
so you see the stderr in your terminal. | |
Will additionally raise CalledProcessError and therefore print 'nihao' | |
e.output is nothing, even though stderr is bound to stdout | |
""" | |
try: | |
subprocess.check_call(['ls', '-al', '/tmp/arstarstarst/'], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT) | |
except subprocess.CalledProcessError as e: | |
print(e.output) # None | |
print('nihao') | |
def tryexc2(): | |
""" | |
Since stderr hasn't been bound, this uses parent's stderr | |
so you see the stderr in your terminal. | |
Will additionally raise CalledProcessError and therefore print 'hello' | |
e.output is None | |
""" | |
try: | |
subprocess.check_call(['ls', '-al', '/tmp/arstarstarst/'], stdout=subprocess.DEVNULL) | |
except subprocess.CalledProcessError as e: | |
print(e.output) # None | |
print('hello') | |
def tryexc_output(): | |
""" | |
Since stderr hasn't been bound, this uses parent's stderr | |
so you see the stderr in your terminal. | |
Will additionally raise CalledProcessError and therefore print 'bonjour' | |
e.output is None | |
""" | |
try: | |
subprocess.check_output(['ls', '-al', '/tmp/rstarst']) | |
except subprocess.CalledProcessError as e: | |
print(e.output) # None | |
print('bonjour') | |
def tryexc_output2(): | |
""" | |
Since stderr has been bound to stdout, it gets | |
so you see the stderr in your terminal. | |
Will additionally raise CalledProcessError and therefore print 'anyonghaseyo' | |
e.output contains the stderr of the subproc | |
""" | |
try: | |
subprocess.check_output(['ls', '-al', '/tmp/rstarst'], stderr=subprocess.STDOUT) | |
except subprocess.CalledProcessError as e: | |
print(e.output) # None | |
print('anyonghaseyo') | |
def tryexc_output3(): | |
""" | |
Since stderr has been bound to stdout, it gets added to e.output if it exists. | |
Since this is a valid call 'guten tag' is never printed and neither is e.output | |
out is the successful output of the call | |
NOTE: not assigning 'out' has the nice effect of `tryexc_output2`, that is, the | |
output is ignored but the errors can be used in logging for example. | |
""" | |
try: | |
out = subprocess.check_output(['ls', '-al', '/tmp/'], stderr=subprocess.STDOUT) | |
except subprocess.CalledProcessError as e: | |
print(e.output) # None | |
print('guten tag') | |
print(out) | |
def simple(): | |
""" | |
Since stderr hasn't been bound, this uses the parent's stderr | |
you see the stderr in your terminal. | |
""" | |
subprocess.check_call(['ls', '-al', '/tmp/arstarstarst/'], stdout=subprocess.DEVNULL) | |
tryexc_output3() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment