gpt4 book ai didi

Linux 终端上的 Python 颜色输出不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 12:13:29 24 4
gpt4 key购买 nike

我有 linux 服务器,我在服务器控制台上运行 python 脚本,我的脚本颜色没有显示。同一脚本适用于 Putty session 。

我在 putty 中检查了 echo $TERM,它是“xterm”,服务器控制台是“linux”。

OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
import os
import subprocess
from subprocess import check_output
from subprocess import call
import sys
import time
from shutil import copyfile
import commands
print OKGREEN + "Testing Green" + ENDC

我也试过在 python 脚本中设置 "os.system('export TERM="xterm"')"但没有工作

谁能帮我解决这个问题。

版本:2.7

注意:在同一个服务器控制台中,vim 以颜色显示

最佳答案

这段代码对我来说效果很好。

jay@Jay-Linux:~$/usr/bin/python -VPython 2.7.15rc1

#!/usr/bin/env python
#Jay
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
print bcolors.OKBLUE + 'OK', bcolors.ENDC
print bcolors.HEADER + 'This is header', bcolors.ENDC
print bcolors.FAIL + 'Failed.', bcolors.ENDC
print bcolors.OKGREEN + 'OK Green', bcolors.ENDC
print bcolors.WARNING + 'Warning', bcolors.ENDC
print bcolors.FAIL + 'Failed', bcolors.ENDC

Output

关于Linux 终端上的 Python 颜色输出不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48137910/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com