Skip to content

Instantly share code, notes, and snippets.

@techtonik
Last active October 1, 2015 10:57
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 techtonik/1978810 to your computer and use it in GitHub Desktop.
Save techtonik/1978810 to your computer and use it in GitHub Desktop.
Python - runout() and runret() functions
""" https://gist.github.com/1978810
shutil.runret() and shutil.runout() functions
runret(command) - run command through shell, return ret code
runout(command) - run command through shell, return output
Public Domain, i.e. free for copy/paste
https://groups.google.com/d/topic/python-ideas/u42aZZnrs8A/discussion
https://bitbucket.org/techtonik/shellrun
"""
import subprocess
def runret(command):
""" Run command through shell, return ret code """
# [ ] silence all output
return subprocess.call(command, shell=True)
def runout(command):
""" Run command through shell, return output (stdout + stderr)
[ ] cyclic buffer reader in separate thread to avoid deadlock
if process writes too much
"""
return subprocess.Popen(command, shell=True, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT).communicate()[0]
@techtonik
Copy link
Author

See https://bitbucket.org/techtonik/shutil-run/ for more pythonic alternative.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment