(python) colour printing with decorator in a function

后端 未结 3 1733
醉酒成梦
醉酒成梦 2020-12-17 05:09

How can I decorate a function so that anything it prints to stdout is in green and anything it prints to stderr is in red<

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-17 05:29

    This works for me in Bash in my Mac's Terminal.app

    import sys
    green = '\033[01;32m'
    red = '\033[01;31m'
    
    sys.stdout.write(green+"Hello ")
    sys.stderr.write(red+"world!")
    

提交回复
热议问题