Count the Number of Lines in a File in Python
To count the number of lines in a file in Python, we can use different methods such as reading the file line by line, using the readlines()
method, or iterating over the file object.
In this tutorial, we will explore multiple approaches to count lines in a file with examples.
Examples for Counting the Number of Lines in a File
1. Using a For Loop to Count Lines in File
We can count the number of lines in a file by iterating over each line using a for
loop and maintaining a counter.

main.py
# Open the file in read mode
with open("sample.txt", "r") as file:
line_count = sum(1 for line in file)
# Print the number of lines
print("Total number of lines:", line_count)
Explanation:
- The
open()
function is used to opensample.txt
in read mode. - The
sum()
function is used with a generator expression that iterates over each line in the file, adding1
for each line. - Finally,
line_count
stores the total number of lines, which is printed.
Output:

2. Using readlines()
Method in Count Lines in a File
The readlines()
method reads all lines into a list, allowing us to count them using len()
.

main.py
# Open the file and read all lines
with open("sample.txt", "r") as file:
lines = file.readlines()
# Count the number of lines
line_count = len(lines)
# Print the number of lines
print("Total number of lines:", line_count)
Explanation:
- The
readlines()
method reads all lines from the file and stores them in a list. - The
len()
function calculates the number of elements (lines) in the list. - The result is stored in
line_count
and printed.
Output:

3. Using enumerate()
Function to Count Lines in File
The enumerate()
function provides an efficient way to count the number of lines while iterating through a file.

main.py
# Open the file and use enumerate to count lines
with open("sample.txt", "r") as file:
line_count = sum(1 for _, _ in enumerate(file, 1))
# Print the number of lines
print("Total number of lines:", line_count)
Explanation:
- The
enumerate()
function iterates over the file, assigning an index to each line (starting from 1). - The
sum()
function adds1
for each line encountered. - The result is stored in
line_count
and printed.
Output:

4. Using While Loop to Count Lines in a File
We can also use a while
loop to read each line manually and count them.

main.py
# Open the file
with open("sample.txt", "r") as file:
line_count = 0
while file.readline():
line_count += 1
# Print the number of lines
print("Total number of lines:", line_count)
Explanation:
- The
readline()
method reads one line at a time. - A
while
loop keeps reading lines until the end of the file is reached. - The
line_count
variable is incremented for each line read. - Finally, the number of lines is printed.
Output:

Conclusion
In this tutorial, we have covered multiple ways to count the number of lines in a file:
- Using a
for
loop: The most memory-efficient method. - Using
readlines()
: Simple but loads the entire file into memory. - Using
enumerate()
: An alternative approach leveraging built-in functions. - Using a
while
loop: Reads one line at a time, similar to afor
loop.
The choice of method depends on the file size and efficiency requirements. The for
loop approach is the most recommended for large files.