Skip to content

Instantly share code, notes, and snippets.

@ap--
Created June 5, 2023 16:57
Show Gist options
  • Save ap--/3053ab5a486efc476869d5541da49ecc to your computer and use it in GitHub Desktop.
Save ap--/3053ab5a486efc476869d5541da49ecc to your computer and use it in GitHub Desktop.
python: count lines in file
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