title: How to preserve python colorama color output when using tee on Windows
body: <p>In my Windows 10 scenario, I want to print arbitrary python console output (<code>print()</code>, <code>sys.exit()</code>, etc) both to console and a log file. I don't have control over some parts of the code (external python packages), so I cannot use some dedicated logging mechanism.</p>
  
  <p>After some research I found the tool <em>tee.exe</em> in UnxUtils which does this task almost the way I want.</p>
  
  <p>My problem is to preserve color as generated by python's <em>colorama</em> package. Is there any way to accomplish that? Currently, tee.exe strips away the color.</p>
  
  <p>The answer I am looking does not have to rely on tee.exe, it's just the closest I got to a real solution. What I am looking for should do the following:</p>
  
  <ul>
  <li>any command line output appears both in the command line and the log file (both STOUT and STDERR)</li>
  <li>the output appears on the command line in real time. Bonus points if this is also true for the log file.</li>
  <li>color is preserved on the command line. Bonus points if the log file does not contain any color-related artifacts.</li>
  </ul>
  
  <p>What I have so far is this:</p>
  
  <p>Python file <em>teetest.py</em>:</p>
  
  <pre><code>import sys
  import colorama
  
  print("Test")
  print("2nd Test")
  
  colorama.init(autoreset=True)
  print(colorama.Fore.RED + 'Color Test')
  colorama.deinit(autoreset=True)
  
  sys.exit("Error_Test")
  </code></pre>
  
  <p>Batch file <em>teetest.bat</em>:</p>
  
  <pre><code>@echo off
  python teetest.py 2&gt;&amp;1 | tee log.txt
  pause
  </code></pre>
  
  <p>My output looks like this (command line and log file are identical, no color):</p>
  
  <pre><code>Test
  2nd Test
  Color Test
  Error_Test
  </code></pre>
  
  <p>The solution I am looking for will print the above to the command line so the words <strong>Color Test</strong> are red.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>It seems that <code>tee.exe</code> is not at fault. Instead, colorama strips away the ANSI characters for color control by design, so the color is lost when passed through <code>tee.exe</code>.</p>
  
  <p>From the colorama manual: </p>
  
  <blockquote>
    <p>Colorama makes this work on Windows, too, by wrapping stdout, stripping ANSI sequences it finds (which would appear as gobbledygook in the output), and converting them into the appropriate win32 calls to modify the state of the terminal. </p>
  </blockquote>
  
  <p>Colorama's <code>init()</code> function offers the parameter <code>strip</code>, which if <code>False</code>, does cause colorama to not strip away the ANSI characters. This in turn allows to write a custom <code>tee.py</code> that does the same as <code>tee.exe</code>, as outlined by user @martineau below. In it, we can call colorama and handle the color properly.</p>
  
  <p>This might be a workable solution, but it still has the downside that I would have to replace all colorama <code>init()</code> calls with <code>init(strip=False)</code> in my original python code and that in turn would cause ANSI characters to appear in the output if the code was called without redirecting through <code>tee.py</code>.</p>
  
  <p>This might actually be the closest we can get to a proper solution here. If anyone can offer other ideas, I'm all ears but I fear chances are slim.</p>