Skip to content

Instantly share code, notes, and snippets.

@lachlan-eagling
Created October 5, 2019 01:45
Show Gist options
  • Select an option

  • Save lachlan-eagling/11847f0c40581da7e7e0af2a44516054 to your computer and use it in GitHub Desktop.

Select an option

Save lachlan-eagling/11847f0c40581da7e7e0af2a44516054 to your computer and use it in GitHub Desktop.
Blog - Generators (File Generator)
def read_chunks(filename, mode="r", chunk_size=32):
with open(filename, mode) as f:
while True:
data = f.read(chunk_size)
if not data:
break
yield data
def main():
filename = "/Users/bob.smith/files/really_really_big.dat"
for chunk in read_chunks(filename):
print(chunk)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment