How do I print colored output to the terminal in Python?
Question:
Is there a Python equivalent of Perl’s
print color 'red';
print <something>;
print color 'reset';
available?
I am using this approach:
"x1b[1;%dm" % (<color code>) + "ERROR: log file does not exist" + "x1b[0m"
what I want is I should be able to set color for all print messages like,
print color 'red'
function_print_something(<some message>)
print color 'reset'
Here ‘function_print_something’ is my python function which will print some formatted log messages on to the screen.
Answers:
Would the Python termcolor module do? This would be a rough equivalent for some uses.
from termcolor import colored
print colored('hello', 'red'), colored('world', 'green')
The example is right from this post, which has a lot more. Here is a part of the example from docs
import sys
from termcolor import colored, cprint
text = colored('Hello, World!', 'red', attrs=['reverse', 'blink'])
print(text)
cprint('Hello, World!', 'green', 'on_red')
A specific requirement was to set the color, and presumably other terminal attributes, so that all following prints are that way. While I stated in the original post that this is possible with this module I now don’t think so. See the last section for a way to do that.
However, most of the time we print short segments of text in color, a line or two. So the interface in these examples may be a better fit than to ‘turn on’ a color, print, and then turn it off. (Like in the Perl example shown.) Perhaphs you can add optional argument(s) to your print function for coloring the output as well, and in the function use module’s functions to color the text. This also makes it easier to resolve occasional conflicts between formatting and coloring. Just a thought.
Here is a basic approach to set the terminal so that all following prints are rendered with a given color, attributes, or mode.
Once an appropriate ANSI sequence is sent to the terminal, all following text is rendered that way. Thus if we want all text printed to this terminal in the future to be bright/bold red, print ESC[
followed by the codes for “bright” attribute (1) and red color (31), followed by m
# print " 33[1;31m" # this would emit a new line as well
import sys
sys.stdout.write(" 33[1;31m")
print "All following prints will be red ..."
To turn off any previously set attributes use 0 for attribute,
Is there a Python equivalent of Perl’s
print color 'red';
print <something>;
print color 'reset';
available?
I am using this approach:
"x1b[1;%dm" % (<color code>) + "ERROR: log file does not exist" + "x1b[0m"
what I want is I should be able to set color for all print messages like,
print color 'red'
function_print_something(<some message>)
print color 'reset'
Here ‘function_print_something’ is my python function which will print some formatted log messages on to the screen.
Would the Python termcolor module do? This would be a rough equivalent for some uses.
from termcolor import colored
print colored('hello', 'red'), colored('world', 'green')
The example is right from this post, which has a lot more. Here is a part of the example from docs
import sys
from termcolor import colored, cprint
text = colored('Hello, World!', 'red', attrs=['reverse', 'blink'])
print(text)
cprint('Hello, World!', 'green', 'on_red')
A specific requirement was to set the color, and presumably other terminal attributes, so that all following prints are that way. While I stated in the original post that this is possible with this module I now don’t think so. See the last section for a way to do that.
However, most of the time we print short segments of text in color, a line or two. So the interface in these examples may be a better fit than to ‘turn on’ a color, print, and then turn it off. (Like in the Perl example shown.) Perhaphs you can add optional argument(s) to your print function for coloring the output as well, and in the function use module’s functions to color the text. This also makes it easier to resolve occasional conflicts between formatting and coloring. Just a thought.
Here is a basic approach to set the terminal so that all following prints are rendered with a given color, attributes, or mode.
Once an appropriate ANSI sequence is sent to the terminal, all following text is rendered that way. Thus if we want all text printed to this terminal in the future to be bright/bold red, print ESC[
followed by the codes for “bright” attribute (1) and red color (31), followed by m
# print " 33[1;31m" # this would emit a new line as well
import sys
sys.stdout.write(" 33[1;31m")
print "All following prints will be red ..."
To turn off any previously set attributes use 0 for attribute,