Created
July 29, 2021 07:56
-
-
Save GluTbl/c032ce897b67e856abe17d5d87ada821 to your computer and use it in GitHub Desktop.
[Execute shell in python]
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 shlex | |
import subprocess | |
def execute_command(command: str, path=None): | |
command_splited = shlex.split(command) | |
if path is None: | |
process = subprocess.Popen(command_splited) | |
else: | |
process = subprocess.Popen(command_splited, cwd=path) | |
process.communicate() | |
def execute_command_silently(command: str, path=None): | |
command_splited = shlex.split(command) | |
if path is None: | |
process = subprocess.Popen(command_splited, | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE) | |
else: | |
process = subprocess.Popen(command_splited, | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE, cwd=path) | |
stdout, stderr = process.communicate() | |
return stdout, stderr | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment