title: python打印提示信息时定义颜色 #标题tags: #标签
date: 2021-12-25
categories: python # 分类

记录下python中用于打印提示信息时定义的函数,可以让提示信息带颜色输出。

  1. import sys
  2. ansi = {
  3. 'underline': '\033[4m',
  4. 'bold': '\033[1m',
  5. 'end': '\033[0m',
  6. 'blue': '\033[34m',
  7. 'red': '\033[31m',
  8. 'green': '\033[32m',
  9. 'purple': '\033[35m'
  10. }
  11. def print_header(msg):
  12. print("\n:# {bold}{underline}{msg}{end}\n".format(msg=msg, **ansi))
  13. def print_info(msg):
  14. print(":: {blue}{msg}{end}".format(msg=msg, **ansi))
  15. def print_warn(msg):
  16. print(":! {purple}{msg}{end}".format(msg=msg, **ansi))
  17. def print_ok(msg):
  18. print(":) {green}{msg}{end}".format(msg=msg, **ansi))
  19. def print_success(msg):
  20. print(":) {green}{msg}{end}".format(msg=msg, **ansi))
  21. def print_error(msg):
  22. print(":( {red}{msg}{end}".format(msg=msg, **ansi))
  23. def print_error_exit(msg, code=1):
  24. print_error(msg)
  25. sys.exit(code)