title: How to detect if the console does support ANSI escape codes in Python?
body: <p>In order to detect if console, correctly <code>sys.stderr</code> or <code>sys.stdout</code>, I was doing the following test:</p>
  
  <pre><code>if hasattr(sys.stderr, "isatty") and sys.stderr.isatty():
     if platform.system()=='Windows':
         # win code (ANSI not supported but there are alternatives)
     else:
         # use ANSI escapes
  else:
     # no colors, usually this is when you redirect the output to a file
  </code></pre>
  
  <p>Now the problem became more complex while running this Python code via an IDE (like PyCharm). Recently PyCharm added support for ANSI, but the first test fails: it has the <code>isatty</code> attribute but it is set to <code>False</code>. </p>
  
  <p>I want to modify the logic so it will properly detect if the output supports ANSI coloring. One requirement is that under no circumstance I should output something out when the output is redirected to a file (for console it would be acceptable).</p>
  
  <h2>Update</h2>
  
  <p>Added more complex ANSI test script at <a href="https://gist.github.com/1316877" rel="noreferrer">https://gist.github.com/1316877</a></p>