title: How can I add a new line of text at top of a file?
body: <p>I'm developing a simple program which makes a Python script executable, and I'm working in the part which adds the interpreter path (<em>#! /usr/bin/python</em>). I tried to do it, but instead of adding a new line, it replaces the current <em>and</em> removes part of the next line. What I'm doing wrong?</p>
  
  <p>I uploaded the source code to Ubuntu Pastebin: <a href="http://pastebin.ubuntu.com/1032683/" rel="noreferrer">http://pastebin.ubuntu.com/1032683/</a> The wrong code is between lines 28 and 31:</p>
  
  <pre><code>wfile = open(file, 'r+')
  if wfile.readline() != "#! /usr/bin/python\n":
      wfile.seek(0)
      wfile.write("#! /usr/bin/python\n")
  </code></pre>
  
  <p>Using Python 2.7.2 with an iPad 2 (<a href="http://pythonforios.com/" rel="noreferrer">Python for iOS</a>), also using 2.5.1 in the same iPad (Cydia port) for testing.</p>