Skip to content

Instantly share code, notes, and snippets.

@PttCodingMan
Last active March 29, 2021 07:02
Show Gist options
  • Save PttCodingMan/056014d4f7e2deb6fbb0835051afb85b to your computer and use it in GitHub Desktop.
Save PttCodingMan/056014d4f7e2deb6fbb0835051afb85b to your computer and use it in GitHub Desktop.
Run command in Python 3 and get the execute result
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