Skip to content

Instantly share code, notes, and snippets.

@ArthurDelannoyazerty
Created March 20, 2024 11:00
Show Gist options
  • Save ArthurDelannoyazerty/61fb02d9dbae92aad7f473bd4a97bcac to your computer and use it in GitHub Desktop.
Save ArthurDelannoyazerty/61fb02d9dbae92aad7f473bd4a97bcac to your computer and use it in GitHub Desktop.
Count the number of line in a file. Useful for large file that cannot fit in memory.
from itertools import takewhile, repeat
def rawincount(filepath:str) -> int:
f = open(filepath, 'rb')
bufgen = takewhile(lambda x: x, (f.raw.read(1024*1024) for _ in repeat(None)))
return sum(buf.count(b'\n') for buf in bufgen)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment