Python: How can I make the ANSI escape codes to work also in Windows?
Question:
If I run this in python under linux it works:
start = " 33[1;31m"
end = " 33[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?
Answers:
You could take a look at https://github.com/kennethreitz/clint
From the readme:
>>> from clint.textui import colored, puts
>>> puts(colored.red('red text'))
red text
# It's red in Windows, OSX, and Linux alike.
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 = " 33[1;31m"
end = " 33[0;0m"
print "File is: " + start + "<placeholder>" + end
I wrote a simple module, available at: http://pypi.python.org/pypi/colorconsole
It works with Windows, Mac OS X and Linux. It uses ANSI for Linux and Mac, but native calls to console functions on Windows. You have colors, cursor positioning and keyboard input. It is not a replacement for curses, but can be very useful if you need to use in simple scripts or ASCII games.
The docs can be found here: http://code.google.com/p/colorconsole/wiki/PageName
PS: This is the same answer for Print in terminal with colors using Python?, but I didn’t know how to link to a reply.
If you are on Win 10 (with native ANSI support in cmd) there seems to be a bug which was marked as resolved in Python 3.7 (though it doesn’t look it was actually fixed).
One workaround is to add subprocess.call('', shell=True)
before printing.
Try adding a semi-colon here
If I run this in python under linux it works:
start = " 33[1;31m"
end = " 33[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?
You could take a look at https://github.com/kennethreitz/clint
From the readme:
>>> from clint.textui import colored, puts
>>> puts(colored.red('red text'))
red text
# It's red in Windows, OSX, and Linux alike.
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 = " 33[1;31m"
end = " 33[0;0m"
print "File is: " + start + "<placeholder>" + end
I wrote a simple module, available at: http://pypi.python.org/pypi/colorconsole
It works with Windows, Mac OS X and Linux. It uses ANSI for Linux and Mac, but native calls to console functions on Windows. You have colors, cursor positioning and keyboard input. It is not a replacement for curses, but can be very useful if you need to use in simple scripts or ASCII games.
The docs can be found here: http://code.google.com/p/colorconsole/wiki/PageName
PS: This is the same answer for Print in terminal with colors using Python?, but I didn’t know how to link to a reply.
If you are on Win 10 (with native ANSI support in cmd) there seems to be a bug which was marked as resolved in Python 3.7 (though it doesn’t look it was actually fixed).
One workaround is to add subprocess.call('', shell=True)
before printing.
Try adding a semi-colon here