title: Python: Open a file, search then append, if not exist
body: <p>I am trying to append a string to a file, if the string doesn't exit in the file. However, opening a file with <code>a+</code> option doesn't allow me to do at once, because opening the file with <code>a+</code> will put the pointer to the end of the file, meaning that my search will always fail. Is there any good way to do this other than opening the file to read first, close and open again to append?</p>
  
  <p>In code, apparently, below doesn't work.</p>
  
  <pre><code>file = open("fileName", "a+")
  </code></pre>
  
  <p>I need to do following to achieve it.</p>
  
  <pre><code>file = open("fileName", "r")
  ... check if a string exist in the file
  file.close()
  
  ... if the string doesn't exist in the file
  file = open("fileName", "a")
  file.write("a string")
  file.close()
  </code></pre>