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

王林
Release: 2023-08-31 16:57:08
forward
916 people have browsed it

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)
Copy after login

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")
Copy after login

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

len()
Copy after login

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' < /b>(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}")
Copy after login

Output

The counting of number of lines is: 6
Copy after login

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}")
Copy after login

Output

Number of lines in the notepad file: 6
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!