title: Python find string in file, edit line and save to a new file
body: <p>I'm trying to go through a text file and find all the lines that start with 'file='. When I find these lines I want to remove all the text in between the dots and finally save that as a new file.</p>
  
  <p>Such lines look like so </p>
  
  <blockquote>
    <p>file="image.&amp;!145.jpg"</p>
  </blockquote>
  
  <p>I'm currently stuck to the place where I either:</p>
  
  <ul>
  <li>delete the text between dots all over the file (not just the lines that start with 'file=')</li>
  <li>delete the text between the dots only on the lines that start with 'file=', but I only end up saving that single line in the new document.</li>
  </ul>
  
  <p>Here's the code that I have so far: </p>
  
  <pre><code>import os
  from os.path import basename
  from re import sub
  
  
  file_in = 'tex_file_01.txt'
  file_out = 'tex_file_01_new.txt'
  
  
  with open(file_in, 'r') as f_in:
      with open(file_out, 'w') as f_out:
          for line in f_in:
              if 'file=' in line:
                  print 'found: ' + line
  
              line_fix = sub('\..*?\.', '.', line)
              print 'fixed: ' + line_fix
  
              f_out.write(line.replace(line, line_fix))
  </code></pre>
  
  <p>The above piece of code deletes the text between the dots all over the file.</p>
  
  <p>Any ideas? Thanks in advance!</p>