title: I need to open and rewrite a line in a file in Python
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/39086/search-and-replace-a-line-in-a-file-in-python">Search and replace a line in a file in Python</a><br>
    <a href="https://stackoverflow.com/questions/125703/how-do-i-modify-a-text-file-in-python">How do I modify a text file in Python?</a>  </p>
  </blockquote>
  
  
  
  <p>I have an input file that I need to rewrite with the different files needed to be modified before running a program. I have tried a variety of the solutions on here but none of them seem to work. I end up just overwriting my file with a blank file</p>
  
  <pre><code>f = open(filename, 'r+')
  text = f.read()
  text = re.sub('foobar', 'bar', text)
  f.seek(0)
  f.write(text)
  f.truncate()
  f.close()
  </code></pre>
  
  <p>Or with that code for instance the name I am changing is different each time I run the program so I need to replace the entire line not just one keyword </p>