(Python) Counting lines in a huge (>10GB) file as fast as possible [duplicate]

Ignacio’s answer is correct, but might fail if you have a 32 bit process. But maybe it could be useful to read the file block-wise and then count the \n characters in each block. def blocks(files, size=65536): while True: b = files.read(size) if not b: break yield b with open(“file”, “r”) as f: print sum(bl.count(“\n”) … Read more