title: Get newline stats for a text file in Python
body: <p>I had a nasty CRLF / LF conflict in git file that was probably committed from Windows machine. Is there a cross-platform way (preferably in Python) to detect what type of newlines is dominant through the file?</p>
  
  <p>I've got this code (based on idea from <a href="https://stackoverflow.com/a/10562258/239247">https://stackoverflow.com/a/10562258/239247</a>):</p>
  
  <pre><code>import sys
  if not sys.argv[1:]:
    sys.exit('usage: %s &lt;filename&gt;' % sys.argv[0])
  
  with open(sys.argv[1],"rb") as f:
    d = f.read()
    crlf, lfcr = d.count('\r\n'), d.count('\n\r')
    cr, lf = d.count('\r'), d.count('\n')
    print('crlf: %s' % crlf)
    print('lfcr: %s' % lfcr)
    print('cr: %s' % cr)
    print('lf: %s' % lf)
    print('\ncr-crlf-lfcr: %s' % (cr - crlf - lfcr))
    print('lf-crlf-lfcr: %s' % (lf - crlf - lfcr))
    print('\ntotal (lf+cr-2*crlf-2*lfcr): %s\n' % (lf + cr - 2*crlf - 2*lfcr))
  </code></pre>
  
  <p>But it gives the stats wrong (for <a href="https://raw.githubusercontent.com/techtonik/spyder/7d819e5dc293103eca1ba1c6c1196489a16d5bd1/spyderlib/widgets/explorer.py" rel="noreferrer">this file</a>):</p>
  
  <pre><code>crlf: 1123
  lfcr: 58
  cr: 1123
  lf: 1123
  
  cr-crlf-lfcr: -58
  lf-crlf-lfcr: -58
  
  total (lf+cr-2*crlf-2*lfcr): -116
  </code></pre>