If the file is significantly large (in GB), and you don’t want to read the whole file to get the line count, This article lets you know how to get the count of lines present in a file in Python.
Table of contents
Steps to Get Line Count in a File
Count Number of Lines in a text File in Python
- Open file in Read Mode
To open a file pass file path and access mode
r
to the open() function.
For example,fp= open(r'File_Path', 'r')
to read a file. - Use for loop with enumerate() function to get a line and its number.
The
enumerate()
function adds a counter to an iterable and returns it in enumerate object. Pass the file pointer returned by theopen()
function to theenumerate()
. Theenumerate()
function adds a counter to each line.
We can use this enumerate object with a loop to access the line number. Return counter when the line ends. - Close file after completing the read operation
We need to make sure that the file will be closed properly after completing the file operation. Use
fp.close()
to close a file.
Example
Consider a file “read_demo.txt.” See an image to view the file’s content for reference.

# open file in read mode
with open(r"E:\demos\files\read_demo.txt", 'r') as fp:
for count, line in enumerate(fp):
pass
print('Total Lines', count + 1)
Output:
Total Lines 8
- The
enumerate()
function adds a counter to each line. - Using enumerate, we are not using unnecessary memory. It is helpful if the file size is large.
- Note:
enumerate(file_pointer)
doesn’t load the entire file in memory, so this is an efficient fasted way to count lines in a file.
Generator and Raw Interface to get Line Count
A fast and compact solution to getting line count could be a generator expression. If the file contains a vast number of lines (like file size in GB), you should use the generator for speed.
This solution accepts file pointer and line count. To get a faster solution, use the unbuffered (raw) interface, using byte arrays, and making your own buffering.
def _count_generator(reader):
b = reader(1024 * 1024)
while b:
yield b
b = reader(1024 * 1024)
with open(r'E:\demos\files\read_demo.txt', 'rb') as fp:
c_generator = _count_generator(fp.raw.read)
# count each \n
count = sum(buffer.count(b'\n') for buffer in c_generator)
print('Total lines:', count + 1)
Output:
Total lines: 8
Use readlines()
to get Line Count
If your file size is small and you are not concerned with performance, then the readlines() method is best suited.
This is the most straightforward way to count the number of lines in a text file in Python.
- The
readlines()
method reads all lines from a file and stores it in a list. - Next, use the
len()
function to find the length of the list which is nothing but total lines present in a file.
Open a file and use the readlines()
method on file pointer to read all lines.
Example:
with open(r"E:\demos\files\read_demo.txt", 'r') as fp:
x = len(fp.readlines())
print('Total lines:', x) # 8
Note: This isn’t memory-efficient because it loads the entire file in memory. It is the most significant disadvantage if you are working with large files whose size is in GB.
Use Loop and Sum Function to Count Lines
You can use the for loop to read each line and pass for loop to sum function to get the total iteration count which is nothing but a line count.
with open(r"E:\demos\files\read_demo.txt", 'r') as fp:
num_lines = sum(1 for line in fp)
print('Total lines:', num_lines) # 8
If you want to exclude the empty lines count use the below example.
with open(r"E:\demos\files\read_demo.txt", 'r') as fp:
num_lines = sum(1 for line in fp if line.rstrip())
print('Total lines:', num_lines) # 8
The in
Operator and Loop to get Line Count
Using in operator and loop, we can get a line count of nonempty lines in the file.
- Set counter to zero
- Use a for-loop to read each line of a file, and if the line is nonempty, increase line count by 1
Example:
# open file in read mode
with open(r"E:\demos\files_demos\read_demo.txt", 'r') as fp:
count = 0
for line in fp:
if line != "\n":
count += 1
print('Total Lines', count)
Count number of lines in a file Excluding Blank Lines
For example, below is the text file which uses the blank lines used to separate blocks.
Jessa = 70 Kelly = 80 Roy = 90 Emma = 25 Nat = 80 Sam = 75
When we use all the above approaches, they also count the blank lines. In this example, we will see how to count the number of lines in a file, excluding blank lines
Example:
count = 0
with open('read_demo.txt') as fp:
for line in fp:
if line.strip():
count += 1
print('number of non-blank lines', count)
Output:
number of non-blank lines 6
Conclusion
- Use
readlines()
or A loop solution if the file size is small. - Use Generator and Raw interface to get line count if you are working with large files.
- Use a loop and
enumerate()
for large files because we don’t need to load the entire file in memory.