Created
March 20, 2024 11:00
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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