How to get line count of a large file cheaply in Python? August 30, 2022 by Tarik One line, probably pretty fast: num_lines = sum(1 for line in open('myfile.txt'))