Created
June 5, 2023 16:57
-
-
Save ap--/3053ab5a486efc476869d5541da49ecc to your computer and use it in GitHub Desktop.
python: count lines in file
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
import mmap | |
def count_lines(fn: str | Path) -> int: | |
"""count lines in file""" | |
with open(fn, "rb") as f: | |
buf = mmap.mmap(f.fileno(), 0, prot=mmap.PROT_READ) | |
return sum(1 for _ in iter(buf.readline, b"")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment