-
-
Save habnabit/671362 to your computer and use it in GitHub Desktop.
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 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