Changing Shell Text Color (Windows)

Question:

I’m looking for a way to change the color of the text output from my python scripts as it runs. The basic idea is something like this:

if (Data < LowerLimit):
    print "Failed" # Output Failed as Red Text
elif (Data > UpperLimit):
    print "Failed" # Red Color
else:
    print "Passed" # Blue Color

The scripts are being used on windows machines for quick data analysis.

Asked By: CyanRook

||

Answers:

Try to look at the following link: Python | change text color in shell

Or read here: http://bytes.com/topic/python/answers/21877-coloring-print-lines

In general solution is to use ANSI codes while printing your string.

There is a solution that performs exactly what you need.

Answered By: Artsiom Rudzenka

Or about the best module I have found
http://pypi.python.org/pypi/colorama

Answered By: Jakob Bowyer

This is extremely simple! Rather than importing odd modules for python or trying long commands you can take advantage of windows OS commands.

In windows, commands exist to change the command prompt text color. You can use this in python by starting with a: import os

Next you need to have a line changing the text color, place it were you want in your code.
os.system('color 4')

You can figure out the other colors by starting cmd.exe and typing color help.

The good part? Thats all their is to it, to simple lines of code.
-Day

Answered By: Daymarquez

Been looking into this for a while and not got any satisfactory answers, however…

1) ANSI escape sequences do work in a terminal on Linux

2) if you can tolerate a limited set of colo(u)rs try this:

print(“hello”, end=”); print(“error”, end=”, file=sys.stderr); print(“goodbye”)

In idle “hello” and “goodbye” are in blue and “error” is in red.

Not fantastic, but good enough for now, and easy!

Answered By: Neil
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.