Skip to content

Instantly share code, notes, and snippets.

@habnabit
Forked from gregglind/shell.py
Created November 10, 2010 19:28
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 habnabit/671362 to your computer and use it in GitHub Desktop.
Save habnabit/671362 to your computer and use it in GitHub Desktop.
import shlex as shlex_
from subprocess import Popen, PIPE
def shell(args, input=None, stdout=PIPE, stderr=PIPE, shlex=False, **kwargs):
"""Gets the output of a command run in a subprocess.
Arguments:
args: A sequence of strings, or a single string if shlex=True.
input: A string to be written to the process's stdin.
Any extra keyword arguments are forwarded to Popen.
Returns:
A tuple of (stdout, stderr)
>>> shell('basename /a/hello', shlex=True)
('hello\n','')
"""
if shlex:
args = shlex_.split(args)
stdin = PIPE if input is not None else None
p = Popen(args, stdin=stdin, stdout=stdout, stderr=stderr, **kwargs)
return p.communicate(input=input)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment