Skip to content

Instantly share code, notes, and snippets.

@mirekfranc
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mirekfranc/4b80080d14aa35db42d2 to your computer and use it in GitHub Desktop.
Save mirekfranc/4b80080d14aa35db42d2 to your computer and use it in GitHub Desktop.
How to do colors in terminal from within python script
#!/usr/bin/env python
import sys, os
from collections import namedtuple
Color = namedtuple ("Color", "PURPLE CYAN DARKCYAN BLUE GREEN YELLOW RED BOLD UNDERLINE END")
color = Color ('\033[95m', '\033[96m', '\033[36m', '\033[94m', '\033[92m', '\033[93m', '\033[91m', '\033[1m', '\033[4m', '\033[0m')
def main ():
output = sys.stdout
if not os.isatty (output.fileno ()):
global color
color = Color ('', '', '', '', '', '', '', '', '', '')
output.write (color.RED + "hello, world" + color.END + "\n")
if __name__ == "__main__":
main ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment