untested. Basically, you want to save cache for "unread" lines. Each time you read a line, if there is something in the cache, first remove it from the cache. If there is nothing in the cache, read the new line from the file. It's rude, but you need to go.
lineCache = [] def pushLine(line): lineCache.append(line) def nextLine(f): while True: if lineCache: yield lineCache.pop(0) line = f.readline() if not line: break yield line return f = open('myfile') for line in nextLine(f):
Michael kent
source share