Preserve colored output from python os.popen()

你说的曾经没有我的故事 提交于 2019-12-10 20:34:23

问题


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.

Example:

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?


回答1:


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.

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

Setting stdout/stderr text color in Windows



来源:https://stackoverflow.com/questions/13299550/preserve-colored-output-from-python-os-popen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!