Python Program 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

Output

3

Using a for loop, the number of lines of a file can be counted.

  • Open the file in read-only mode.
  • Using a for loop, iterate through the object f.
  • In each iteration, a line is read; therefore, increase the value of loop variable after each iteration.

Example 2: Using list comprehension

Output

3
  • Open the file in read-only mode.
  • Using a for loop, iterate through open('my_file.txt').
  • After each iteration, return 1.
  • Find the sum of the returned values.