Skip to content

Instantly share code, notes, and snippets.

@joocer
Last active October 24, 2020 09:48
Show Gist options
  • Save joocer/262cb1db0dd1164d193f8605fced50a8 to your computer and use it in GitHub Desktop.
Save joocer/262cb1db0dd1164d193f8605fced50a8 to your computer and use it in GitHub Desktop.
read an arbitrary long file in python, line by line
def read_file(filename, chunk_size=1024*1024, delimiter='\n'):
with open(filename, 'r', encoding="utf8") as f:
carry_forward = ''
chunk = 'INITIALIZED'
while len(chunk) > 0:
chunk = f.read(chunk_size)
augmented_chunk = carry_forward + chunk
lines = augmented_chunk.split(delimiter)
carry_forward = lines.pop()
yield from lines
if carry_forward:
yield carry_forward
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment