Is there a way to go back when reading a file using seek and calls to next()?
问题 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 here that I could use something like 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() Yet, the structure of my current code is something like the