Undo a file readline() operation so file-pointer is back in original state Undo a file readline() operation so file-pointer is back in original state python python

Undo a file readline() operation so file-pointer is back in original state


You have to remember the position by calling file.tell() before the readline and then calling file.seek() to rewind. 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()

I can't recall if it is safe to call file.seek() inside of a for line in file loop so I usually just write out the while loop. There is probably a much more pythonic way of doing this.


You record the starting point of the line with thefile.tell() before you call readline, and get back to that point, if you need to, with thefile.seek.

>>> with open('bah.txt', 'w') as f:...   f.writelines('Hello %s\n' % i for i in range(5))... >>> with open('bah.txt') as f:...   f.readline()...   x = f.tell()...   f.readline()...   f.seek(x)...   f.readline()... 'Hello 0\n''Hello 1\n''Hello 1\n'>>> 

as you see, the seek/tell "pair" is "undoing", so to speak, the file pointer movement performed by readline. Of course, this can only work on an actual seekable (i.e., disk) file, not (e.g.) on file-like objects built w/the makefile method of sockets, etc etc.


If your method simply wants to iterate through the file, then you could use itertools.chain to make an appropriate iterator:

import itertools# do something to the marker line and everything afterdef process(it):    for line in it:        print line,        with open(filename,'r') as f:    for line in f:        if 'marker' in line:            it=itertools.chain((line,),f)            process(it)            break