保存从python os.popen()的彩色输出 [英] Preserve colored output from python os.popen()

查看:561
本文介绍了保存从python os.popen()的彩色输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Python从给定的目录使用msbuild.exe构建多个VS解决方案。我只想要具体的输出,但为了节省工作,解释结果的最好方法是使用原始的ANSI编码。

I'm using Python to build multiple VS solutions from a given directory using msbuild.exe. I only want specific output, but to save work, the best way to interpret the results is with the original ANSI encoding.

示例:

import os
output = os.popen("MSBuild VSTests.sln").read()
print output

这将打印输出,但没有颜色。有什么办法保护这个?

This prints the output, but without color. Is there any way to preserve this?

推荐答案

据我所知,你不能。颜色是控制台的属性,而不是输出文本本身的属性。它不像在Linux中,你在文本中设置的颜色和阅读,回到控制台保存颜色的转义字符。

You can't, as far as I know. The color is a property of the console and not of the output text itself. It's not like in Linux where you get escape characters in the text that set the color and reading that out back to console preserves color.

另一个副作用是,你不能具有 STDOUT STDERR 的单独颜色,如此SO问题所示:

Another side effect of this is that you can't have a separate color for STDOUT and STDERR as shown in this SO question:

设置stdout / err文本颜色窗口C ++

这篇关于保存从python os.popen()的彩色输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆