Skip to content

Instantly share code, notes, and snippets.

@agmangas
Created December 3, 2018 16:01
Show Gist options
  • Save agmangas/fb12f2621c54ca7d5ac6fd3cfd6511bf to your computer and use it in GitHub Desktop.
Save agmangas/fb12f2621c54ca7d5ac6fd3cfd6511bf to your computer and use it in GitHub Desktop.
Popen.communicate() wrapper function
from subprocess import Popen, PIPE
def _popen_communicate(cmd_args):
"""Takes a list of arguments and executes a command.
Blocks waiting for the execution to finish.
Returns a tuple containing the stdout and stderr."""
process = Popen(cmd_args, stdout=PIPE, stderr=PIPE)
stdout, stderr = process.communicate()
if process.returncode != 0:
raise Exception("'{}' returned error code: {}".format(' '.join(cmd_args), process.returncode))
return stdout, stderr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment