Determining the line count of massive files is crucial for various applications. While conventional approaches using for loops can be time-consuming and memory-intensive, Python offers an optimized solution.
The following code snippet showcases a single-line approach for line counting, outperforming the loop method presented in the question:
num_lines = sum(1 for _ in open('myfile.txt'))
This code leverages Python's generator expression, which iterates over each line in the file, without holding all lines in memory. The sum(1 for _ in ...) construction counts the number of lines.
For further speed optimization and increased robustness, consider the following enhancements:
An updated code snippet with these enhancements:
with open("myfile.txt", "rb") as f: num_lines = sum(1 for _ in f)
For the deprecated rbU mode in Python 3.3 and later, use rb instead. This mode is removed in Python 3.11.
The above is the detailed content of How Can Python Efficiently Count Lines in Very Large Files?. For more information, please follow other related articles on the PHP Chinese website!