Skip to content

Instantly share code, notes, and snippets.

@iTrooz
Last active June 21, 2024 13:29
Show Gist options
  • Save iTrooz/d56cca91e8a64e9a4fef456a42cb792b to your computer and use it in GitHub Desktop.
Save iTrooz/d56cca91e8a64e9a4fef456a42cb792b to your computer and use it in GitHub Desktop.
Run python shell command, return merged stdout and stderr, and exit code
import subprocess
from typing import Tuple
def run_cmd(cmd: str) -> Tuple[int, str]:
p = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output, _ = p.communicate()
return p.returncode, output.decode("utf-8")
print(run_cmd("ls /"))
print(run_cmd("ls /does-not-exist"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment