title: How to compare all lines in some file with another line?
body: <p>I am new at Python and need some help. </p>
  
  <p>I have a file with <code>x</code> number of lines. I want to compare each line of that file with another line, and write that line to that file if they are different.</p>
  
  <p>I looked for an answer but didn't find anything that I can use.  I tried something myself but it doesn't work.</p>
  
  <p>My code:</p>
  
  <pre><code>filename = ...
  my_file = open(filename, 'r+')
  for line in my_file:
      new_line = ("text")
      print line
      print new_line
      if new_line == line:
          print('same')
      else:
          print('diffrent')
          my_file.write('%s' % (new_line))
  </code></pre>
  
  <p>I only want my application to write the line to the file if it doesn't already exist there.</p>
  
  <pre><code>contents of filename
  ====================
  text
  text1
  text2
  </code></pre>
  
  <p>In the case above where new line is "text", the application shouldn't do anything because that line already exist in the file. However, if the new line is "text3" then it should be written to the file as follows:</p>
  
  <pre><code>contents of filename
  ====================
  text
  text1
  text2
  text3
  </code></pre>