简体   繁体   English

为什么用Python没有颜色输出?

[英]why no color output with Python?

I have a batch file which calls .Net solution to build the project. 我有一个批处理文件,它调用.Net解决方案来构建项目。 On the dos console window, the warning and error will be in different color, green and red, looks nice. 在dos控制台窗口上,警告和错误将使用不同的颜色(绿色和红色),看起来不错。 When the batch file called from Python, no color at all, all in a single color. 从Python调用批处理文件时,根本没有颜色,所有颜色都是一种颜色。 Is it possible to get the same colorful result with python? 使用python可以获得相同的彩色结果吗? my call to the batch file is like this: 我对批处理文件的调用是这样的:

p = subprocess.Popen('manualBuild.bat', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) 
for line in p.stdout.readlines(): 
    print line, 
retval = p.wait()

thanks 谢谢

Some programs test to see if they're actually connected to a console/terminal, and suppress attribute changes if they're not in order to make it easier to parse/process the output. 一些程序进行测试以查看它们是否实际上已连接到控制台/终端,并抑制属性更改(如果未连接)以使其更易于解析/处理输出。 I know that on *nix systems you can use unbuffer to fool the program, but I don't know if there's a Windows equivalent. 我知道在* nix系统上可以使用unbuffer欺骗程序,但是我不知道是否有Windows等效程序。

If this were UNIX, I'd suggest you don't get the output in a variable but just let the program print it out (ie. don't pass stdout to subprocess). 如果是UNIX,我建议您不要在变量中获取输出,而应让程序将其打印出来(即,不要将stdout传递给子进程)。 Maybe you can try that once? 也许您可以尝试一次?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM