title: Python write a string to a file and keep \r
body: <p>In python (2.7 or 3), say I have a string with <code>\r</code> in it, and I want to write it to a file and <em>keep</em> the <code>\r</code>. The default behavior is to replace the <code>\r</code> with <code>\n</code>. How can I do this? For example:</p>
  
  <pre><code>f= open("file.txt","w+")
  f.write('foo\rbar')
  f.close()
  </code></pre>
  
  <p>Leaves me with a file with text <code>foo\nbar</code>. I've read about universal newline handling, and I think I can use <code>newline=''</code> as an option for <code>open()</code> when opening a file with <code>\r</code> if I want to keep the <code>\r</code>. But <code>write()</code> does not accept the <code>newline</code> option, and I'm at a loss.</p>
  
  <p>I have an application where the <code>\r</code> is meaningful, distinct from <code>\n</code>. And I need to be able to write to file and keep <code>\r</code> as it is.</p>