Skip to content

Instantly share code, notes, and snippets.

@Hansimov
Last active August 15, 2023 06:29
Show Gist options
  • Save Hansimov/b2f9542b43703219e3f431b85e805a5b to your computer and use it in GitHub Desktop.
Save Hansimov/b2f9542b43703219e3f431b85e805a5b to your computer and use it in GitHub Desktop.
python run shell command and output to the console
# Color print
def color_str(s, level="info"):
msg_level_D = {
"shell": "1;36;40", # cyan
"success": "1;32;40", # green
"info": "1;34;40", # blue
"warn": "1;35;40", # magenta
"error": "1;31;40", # red
"hint": "1;33;40", # yellow
"mark": "1;37;42", # white + green
}
cb = "\x1b[{}m"
ce = "\x1b[0m"
cb = cb.format(msg_level_D[level])
return f"{cb}{s}{ce}"
def color_print(s, level="info"):
print(color_str(s, level=level))
# Shell command
def shell_cmd(cmd, getoutput=False, showcmd=True):
if showcmd:
color_print(f"\n$ [{os.getcwd()}]", "info")
color_print(f" $ {cmd}\n", "shell")
if getoutput:
output = subprocess.getoutput(cmd)
# print(output)
return output
else:
subprocess.run(cmd, shell=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment