title: Don't write final new line character to a file
body: <p>I have looked around StackOverflow and couldn't find an answer to my specific question so forgive me if I have missed something.</p>
  
  <pre><code>import re
  
  target = open('output.txt', 'w')
  
  for line in open('input.txt', 'r'):
      match = re.search(r'Stuff', line)
      if match:
          match_text = match.group()
          target.write(match_text + '\n')
      else:
          continue
  target.close()
  </code></pre>
  
  <p>The file I am parsing is huge so need to process it line by line.</p>
  
  <p>This (of course) leaves an additional newline at the end of the file.</p>
  
  <p>How should I best change this code so that on the final iteration of the 'if match' loop it doesn't put the extra newline character at the end of the file. Should it look through the file again at the end and remove the last line (seems a bit inefficient though)?</p>
  
  <p>The existing StackOverflow questions I have found cover removing all new lines from a file.</p>
  
  <p>If there is a more pythonic / efficient way to write this code I would welcome suggestions for my own learning also.</p>
  
  <p>Thanks for the help!</p>