title: Python file.tell gives wrong value location
body: <p>I am trying to extract a number of locations from an existing file using Python. This is my current code for extracting the locations: </p>
  
  <pre><code>    self.fh = open( fileName , "r+")
      p = re.compile('regGen regPorSnip begin')
      for line in self.fh :
          if ( p.search(line) ):
              self.porSnipStartFPtr = self.fh.tell()
              sys.stdout.write("found regPorSnip")
  </code></pre>
  
  <p>This snippet is repeated a number of times (less the file open) with different search values, and seems to work: I get the correct messages, and the variables have values. </p>
  
  <p>However, using the code below, the first write location is wrong, while subsequent write locations are correct:</p>
  
  <pre><code>    self.fh.seek(self.rstSnipStartFPtr,0)
      self.fh.write(str);
      sys.stdout.write("writing %s" % str )
      self.rstSnipStartFPtr = self.fh.tell()
  </code></pre>
  
  <p>I have read that passing certain <code>read</code>/<code>readline</code> options to <code>fh</code> can cause an erroneous tell value because of Python's tendency to 'read ahead'.  One suggestion I saw for avoiding this is to read the whole file and rewrite it, which isn't a very appealing solution in my application.</p>
  
  <p>If i change the first code snippet to:</p>
  
  <pre><code>  for line in self.fh.read() :
          if ( p.search(line) ):
              self.porSnipStartFPtr = self.fh.tell()
              sys.stdout.write("found regPorSnip")
  </code></pre>
  
  <p>Then it appears that <code>self.fh.read()</code> is returning only characters and not an entire line. The search never matches. The same appears to hold true for <code>self.fh.readline()</code>.</p>
  
  <p>My conclusion is that <code>fh.tell</code> only returns valid file locations when queried after a write operation.</p>
  
  <p>Is there a way to extract the accurate file location when reading/searching?</p>
  
  <p>Thanks.</p>