Skip to content

Instantly share code, notes, and snippets.

@GluTbl
Created July 29, 2021 07:56
Show Gist options
  • Save GluTbl/c032ce897b67e856abe17d5d87ada821 to your computer and use it in GitHub Desktop.
Save GluTbl/c032ce897b67e856abe17d5d87ada821 to your computer and use it in GitHub Desktop.
[Execute shell in python]
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