Last active
March 29, 2021 07:02
-
-
Save PttCodingMan/056014d4f7e2deb6fbb0835051afb85b to your computer and use it in GitHub Desktop.
Run command in Python 3 and get the execute result
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 subprocess | |
import locale | |
def run_cmd(cmd, use_cmd=False): | |
if use_cmd: | |
# windows | |
cmd = ['cmd', '/c'] + cmd | |
# Linux | |
cmd = ['/bin/sh', '-c'] + cmd | |
result = subprocess.run(cmd, stdout=subprocess.PIPE) | |
result = result.stdout | |
result = result.replace(b'\r', b'') | |
result = result.decode(locale.getpreferredencoding()) | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment