Python Program to Count number of lines present in the file


In Python, we have some file in-built functions that can be used to count the number of lines present in the file. We will open the notepad and write some lines of text into it. Then use this file in the file handling program in Python to count the total number of lines present in the file. The ‘r’ mode is defined by reading the file's text.

Syntax

The following syntax is used in the example is −

with open("file_name.txt", mode)

The open method is used for opening a file in Python. This accepts the two parameters −

  • file_name.txt − Mention the file name.

  • mode − This parameter determine the file positioned and what the method allows.

readlines("write some text")

The readlines() method return the list of text lines that exist in a file.

len()

The len method is used to return the length of the variable.

Algorithm

The following steps are −

  • Start the ‘with open()’ method which accepts two parameters- ‘tutebox.txt’(filename) and ‘r’(read the file). Mention the object name as ‘file’ to work on the iteration of each line in for loop.

  • Then initialize the variable named ‘cnt’ to the value ‘0’ that will keep track of the count line from the beginning. [ Example 1 ]

  • Then use the built-in method readlines() and stored it in the variable li. [ Example 2 ]

  • Next, use the for loop in the file to iterate each line and then increments the count by plus 1 which will count the total line. [Example 1]

  • Next calculate the total length using len that accepts the parameter named li and store it in total_line.[ Example 2 ]

  • Print the result with the help of a variable named ‘cnt’.[ Example 1]

  • Finally, we are printing the result with the help of the variable total_line.[ Example 2 ]

Example 1

In this program, we create the object named file to work for with open() method that will read the file. For counting the number of lines it will increment the count by adding 1.

#Count the number of lines in python
with open('tutebox.txt','r') as file:
   cnt = 0
   for line in file: 
      cnt += 1
print(f"The counting of number of lines is: {cnt}")

Output

The counting of number of lines is: 6

Example 2

In this program, we use mode ‘r’ of file handling in Python to read the text from a file. For counting the line it uses the ‘readlines()’ method and returns the total count number by the ‘len()’ method.

with open('tutebox.txt','r') as file:
   li = file.readlines()
total_line = len(li)
print(f"Number of lines in the notepad file: {total_line}")

Output

Number of lines in the notepad file: 6

Conclusion

We saw the difference between two examples by applying mode ‘r’ to the file. Both examples show the opening file using with open() method. Example 1 used the concept of for loop to find the total count number of lines present in a file whereas Example 2 used the concept of a predefined method in Python.

Updated on: 01-Jun-2023

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements