Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print original color output using subprocess.check_output

Tags:

python

I have bunch of executable running GTEST.

GTEST gives color output of passes and failes tests.

Now when I try to run bunch of executable using python subprocess.check_output script it is printing without color.

Here is code:

for root, dirs, files in os.walk(path):
    for file in files:
       try:
           print subprocess.check_output([os.path.join(root, file)], shell= True, )
       except subprocess.CalledProcessError, e:
            error +=1
print error

Can anyone please help me, How I can print output as original output with colors?

like image 597
rovy Avatar asked Dec 10 '25 10:12

rovy


1 Answers

Since I run in a similiar problem today on windows. I did a little resarch and it seems, that the problem is, that gtest creates the colors within the console by using the screenbuffer handles. The plain outstream of gtest does not contain any color information. That means that the color is lost as soon as these are redirected.

The following code worked for me:

subprocess.check_call(["cmd", "/c", gtest_case_executable, "--gtest_color=yes"]) 

as long as the Python command line call was not executed with any redirectioning calls like tee or >>.

like image 112
Stefan Avatar answered Dec 15 '25 16:12

Stefan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!