title: Deleting a line from a file in Python
body: <p>I'm trying to delete a specific line that contains a specific string.</p>
  
  <p>I've a file called <strong>numbers.txt</strong> with the following content:</p>
  
  <blockquote>
    <p>peter<br>
    tom<br>
    tom1<br>
    yan<br></p>
  </blockquote>
  
  <p>What I want to delete is that <strong>tom</strong> from the file, so I made this function:</p>
  
  <pre><code>def deleteLine():
  fn = 'numbers.txt'
  f = open(fn)
  output = []
  for line in f:
      if not "tom" in line:
          output.append(line)
  f.close()
  f = open(fn, 'w')
  f.writelines(output)
  f.close()
  </code></pre>
  
  <p>The output is:</p>
  
  <blockquote>
    <p>peter<br>
    yan<br></p>
  </blockquote>
  
  <p>As you can see, the problem is that the function delete <strong>tom</strong> and <strong>tom1</strong>, but I don't want to delete <strong>tom1</strong>. I want to delete just <strong>tom</strong>. This is the output that I want to have:</p>
  
  <blockquote>
    <p>peter<br>
    tom1<br>
    yan<br></p>
  </blockquote>
  
  <p>Any ideas to change the function to make this correctly?</p>