Home  >  Article  >  Backend Development  >  Python program to count the number of lines present in a file

Python program to count the number of lines present in a file

王林
王林forward
2023-08-31 16:57:08899browse

Python program to count the number of lines present in a file

In Python, we have some file built-in functions that can be used to count the number of lines present in a file. We will open Notepad and write some lines of text in it. This file is then used in Python's file handler to count the total number of lines present in the file. 'r' mode is defined by reading the text of a file.

grammar

The following syntax is used in the example -

with open("file_name.txt", mode)

The open method is used to open files in Python. It accepts two parameters -

  • file_name.txt − Mention the file name.

  • mode − This parameter determines the location of the file and what is allowed for the method.

readlines("write some text")

readlines()The method returns a list of text lines present in the file.

len()

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' (file name) and 'r' (read file). Specify the object name as 'file' to handle the iteration of each row in the for loop.

  • The variable named 'cnt' is then initialized to the value '0', which will track the count lines from the beginning. 【Example 1】

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

  • Next, calculate the total number of rows by iterating over each row using a for loop in the file and adding 1 to the count. [Example 1]

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

  • Use a variable named 'cnt' to print the results. [Example 1]

  • Finally, we print the results with the help of the variable total_line. [Example 2]

Example 1

In this program, we create an object named file for reading the file using the open() method. To count the number of rows, 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 Python's file processing mode 'r' to read text from a file. To count the number of lines, it uses the 'readlines()' method and returns the total number of lines via 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

in conclusion

We see the difference between the two examples by applying mode 'r' to the file. Both examples use the with open() method to open the file. Example 1 uses the concept of for loop to find the total number of lines present in the file, while example 2 uses the concept of predefined methods in Python.

The above is the detailed content of Python program to count the number of lines present in a file. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete