How to get line count of a large file cheaply in Python?

You can use the line_count() function from the itertools module to get the line count of a large file cheaply in Python.

from itertools import count

def line_count(file_path):
    with open(file_path, "r") as f:
        return sum(1 for _ in f)

This function reads the file line by line and returns the number of lines in the file. Since it reads the file one line at a time, it can handle large files efficiently without loading the entire file into memory.

You can also use the os.popen() function to get the line count of a large file cheaply in python:

import os

def line_count(file_path):
    return int(os.popen(f'wc -l {file_path}').read().split()[0])

This function uses the wc command, which is available on most UNIX-like systems and provides a fast way to count the number of lines in a file.