Join our newsletter for the latest updates.

Python Program to Get Line Count of a File

In this example, you will learn to get line count of a file.

To understand this example, you should have the knowledge of the following Python programming topics:


Example 1: Using a for loop

The content of the file my_file.txt is

honda 1948
mercedes 1926
ford 1903

Source Code

def file_len(fname):
    with open(fname) as f:
        for i, l in enumerate(f):
            pass
    return i + 1

print(file_len("my_file.txt"))

Output

3

Example 2: Using list comprehension

num_of_lines = sum(1 for l in open('my_file.txt'))

print(num_of_lines)

Output

3
Did you find this article helpful?