title: Preserve end-of-line style when working with files in python
body: <p>I am looking for a way to ensure that the end-of-line style of a file is maintained in python program while reading, editing and writing.</p>
  
  <p>Python has universal file ending support, which can convert all line endings to <code>\n</code> when the file is read, and then convert them all to the system default when the file is written.  In my case I would like to still do the initial conversion, but then write the file with the original EOL style rather than the system default.</p>
  
  <p>Is there a standard way to do this kind of thing?  If not, is there a standard way to detect the EOL style of a file?</p>
  
  <p>Assuming that there is no standard way to do this, a possible work flow would be:</p>
  
  <ol>
  <li>Read in a file in binary mode.</li>
  <li>Decode into utf-8 (or whatever encoding is required).</li>
  <li>Detect EOL style.</li>
  <li><p>Convert all line endings to <code>\n</code>.</p></li>
  <li><p>Do stuff with the file.</p></li>
  <li><p>Convert all line endings to original style.</p></li>
  <li>Encode file.</li>
  <li>Write file in binary mode.</li>
  </ol>
  
  <p>In this work flow, what is the best way to do step 2?</p>