Python: Undo a file readline() operation so file-pointer is back in original state
I'm browsing through a Python file pointer of a text file in read-only mode using file.readline() looking for a special line. Once I find that line I want to pass the file pointer to a method that is expecting the file pointer to be at the START of that readline (not right after it.) How do I essentially undo one file.readline() operation on a file pointer? 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