默认情况下python的print()重定向到文件会禁用ANSI颜色,但可通过三种方法显式保留:一、直接write带ANSI序列的字符串;二、用colorama.init(strip=False)或rich.console(force_terminal=True);三、临时patch sys.stdout.isatty()。
默认情况下,Python 的 print() 函数将内容输出到终端时能正常显示 ANSI 颜色(如
print()