Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python in terminal - how to get current colour?

I have read a couple of URLs about setting colour in terminal. But after a colour change a while later I'd like to reset into previous colour. How can I get current colour ? (I'd like to avoid third party libraries and use only batteries included ;-)) Especially (from (python) colour printing with decorator in a function ):

import sys
green = '\033[01;32m'
red = '\033[01;31m'

... remember current colours here ...
sys.stdout.write(green+"Hello ")
sys.stderr.write(red+"world!")
like image 340
pb100 Avatar asked Jul 15 '26 07:07

pb100


1 Answers

You can return default color the same way you colorize your texts:

native = '\033[m'
sys.stdout.write(native)

Thus temporary coloring may be achieved with

print green + 'Hello' + native
like image 89
Rostyslav Dzinko Avatar answered Jul 17 '26 19:07

Rostyslav Dzinko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!