title: Is there a way to go back when reading a file using seek and calls to next()?
body: <p>I'm writing a Python script to read a file, and when I arrive at a section of the file, the final way to read those lines in the section depends on information that's given also in that section. So I found <a href="https://stackoverflow.com/questions/3505479/python-undo-a-python-file-readline-operation-so-file-pointer-is-back-in-origi">here</a> that I could use something like</p>
  
  <pre><code>fp = open('myfile')
  last_pos = fp.tell()
  line = fp.readline()
  while line != '':
    if line == 'SPECIAL':
    fp.seek(last_pos)
    other_function(fp)
    break
  last_pos = fp.tell()
  line = fp.readline()
  </code></pre>
  
  <p>Yet, the structure of my current code is something like the following:</p>
  
  <pre><code>fh = open(filename)
  
  # get generator function and attach None at the end to stop iteration
  items = itertools.chain(((lino,line) for lino, line in enumerate(fh, start=1)), (None,))
  item = True
  
    lino, line = next(items)
  
    # handle special section
    if line.startswith['SPECIAL']:
  
      start = fh.tell()
  
      for i in range(specialLines):
        lino, eline = next(items)
        # etc. get the special data I need here
  
      # try to set the pointer to start to reread the special section  
      fh.seek(start)
  
      # then reread the special section
  </code></pre>
  
  <p>But this approach gives the following error:</p>
  
  <blockquote>
    <p>telling position disabled by next() call</p>
  </blockquote>
  
  <p>Is there a way to prevent this?</p>