Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # No dependencies needed.
- # G.Berthiaume 2019
- import pathlib
- import subprocess
- import shlex
- from typing import Tuple # mypy
- def execute_command(cmd: str, cwd: pathlib.Path = pathlib.Path(".")) -> Tuple[str, str]:
- """ Execute a child program in a new process and pipe the output out.
- Args:
- cmd: The command to execute. eg. `git status`
- cwd: Where to start the child program. The default is in the same directory
- as this python script.
- Returns:
- A tuple of two strings: The standard output and the standard error
- of the child program.
- If there's no stdout, stdout will be an empty string.
- If there's no stderr, stderr will be an empty string.
- """
- PIPE = subprocess.PIPE
- process = subprocess.Popen(
- shlex.split(cmd), stdout=PIPE, stderr=PIPE, cwd=str(cwd.resolve())
- )
- stdout_raw, stderr_raw = process.communicate()
- stdout, stderr = stdout_raw.decode("utf-8"), stderr_raw.decode("utf-8")
- return stdout, stderr
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement