Last active
June 21, 2024 13:29
-
-
Save iTrooz/d56cca91e8a64e9a4fef456a42cb792b to your computer and use it in GitHub Desktop.
Run python shell command, return merged stdout and stderr, and exit code
This file contains hidden or 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 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