Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A simple file wrapper to allow seeking to specific lines at random.
class LineSeekableFile:
def __init__(self, seekable):
self.fin = seekable
self.line_map = list() # Map from line index -> file position.
self.line_map.append(0)
while seekable.readline():
self.line_map.append(seekable.tell())
def __getitem__(self, index):
# NOTE: This assumes that you're not reading the file sequentially. For that, just use 'for line in file'.
self.fin.seek(self.line_map[index])
return self.fin.readline()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.