Python: How can I make the ANSI escape codes to work also in Windows?

Eduard Florinescu picture Eduard Florinescu · Sep 19, 2012 · Viewed 40.3k times · Source

If I run this in python under linux it works:

start = "\033[1;31m"
end = "\033[0;0m"
print "File is: " + start + "<placeholder>" + end

But if I run it in Windows it doesn't work, how can I make the ANSI escape codes work also on Windows?

Answer

pr0gg3d picture pr0gg3d · Sep 19, 2012

You could check Python module to enable ANSI colors for stdout on Windows? to see if it's useful.

The colorama module seems to be cross-platform.

You install colorama:

pip install colorama

Then:

import colorama
colorama.init()
start = "\033[1;31m"
end = "\033[0;0m"
print "File is: " + start + "<placeholder>" + end