Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print cmd with colors

I am trying to get colored command line output. I was able to get colored Python console output using colorama with this:

from colorama import Fore
from colorama import Style

print(f'{Fore.GREEN}A')
print(f'{Fore.RED}B')
print('C')
print(f'{Style.RESET_ALL}D')
print('E')

This perfectly works inside the Python Console in PyCharm. However, if I run the program under Windows cmd. There is no color at all but the colorama text is just added without any effect:

←[32mA
←[31mB
C
←[0mD
E

Can I modify the code so that it also works in Windows cmd?

like image 822
mrCarnivore Avatar asked Nov 02 '25 13:11

mrCarnivore


1 Answers

You'll need to add convert=True to your colorama init call:

from colorama import Fore, Style, init

init(convert=True)

print(f'{Fore.GREEN}A')
print(f'{Fore.RED}B')
print('C')
print(f'{Style.RESET_ALL}D')
print('E')
like image 109
Jeremiah Avatar answered Nov 04 '25 04:11

Jeremiah