title: Editing specific line in text file in Python
body: <p>Let's say I have a text file containing:</p>
  
  <pre class="lang-none prettyprint-override"><code>Dan
  Warrior
  500
  1
  0
  </code></pre>
  
  <p>Is there a way I can edit a specific line in that text file? Right now I have this:</p>
  
  <pre><code>#!/usr/bin/env python
  import io
  
  myfile = open('stats.txt', 'r')
  dan = myfile.readline()
  print dan
  print "Your name: " + dan.split('\n')[0]
  
  try:
      myfile = open('stats.txt', 'a')
      myfile.writelines('Mage')[1]
  except IOError:
          myfile.close()
  finally:
          myfile.close()
  </code></pre>
  
  <p>Yes, I know that <code>myfile.writelines('Mage')[1]</code> is incorrect. But you get my point, right? I'm trying to edit line 2 by replacing Warrior with Mage. But can I even do that?</p>