Write a python program to count the number of lines in a text file – Python Program to Count the Number of Lines in a Text File

Write a python program to count the number of lines in a text file: Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.

Files in Python:

Python File handling is a method that allows you to save the program’s output to a file or read data from a file. In the programming world, file handling is a critical notion. File management is employed in practically every type of project. For example, suppose you’re developing an inventory management system. You have data connected to sales and purchases in the inventory management system, thus you must save that data somewhere. You can save that data to a file using Python file management. If you want to undertake data analysis, you must be given data in the form of a comma-separated file or a Microsoft Excel file. You can read data from a file and also store output back into it using file handling.

Given a file, the task is to count the entire number of lines within the given file in Python.

Program to Count the Number of Lines in a Text File

Below is the full approach to count the total number of lines in the given text file.

Approach:

  • Take a variable that stores the count of the number of lines in a given file and initialize it to 0.
  • Create the file or upload the existing file.
  • Enter the file name of the file using the input() function and store it in a variable.
  • In read mode, open the file with the entered file name.
  • Using for loop, Traverse the lines in the file.
  • Increment the value of line count by 1.
  • Print the line count.
  • Exit of Program

Below is the implementation:

# Take a variable that stores the count of a number of lines in a given file and initialize it to 0.
linecount = 0
# Enter the file name of the  file using the input() function and store it in a variable.
filename = input("Enter the file name = ")
# In read mode, open the file with the entered file name.
with open(filename, 'r') as givenfile:
    # Using for loop, Traverse the lines in the file.
    # Increment the value of line count by 1.
    for line in givenfile:
        linecount = linecount+1
# Print the line count.
print('The total number of lines in the given file = ', linecount)

Output:

Enter the file name = hello.txt 
The total number of lines in the given file = 7

Explanation:

  • A file name must be entered by the user.
  • In read mode, the file is opened with the open() method.
  • To read through each line of the file, a for loop is utilized.
  • Each time, the line count is incremented, and the total count is printed using the print() function.

File Content:

Python File handling is a method that allows you to save the program's output to a file or read data from a file. 
In the programming world, file handling is a critical notion. File management is employed in practically every type 
of project. For example, suppose you're developing an inventory management system. You have data connected to sales 
and purchases in the inventory management system, thus you must save that data somewhere. You can save that data to a
file using Python file management. If you want to undertake data analysis, you must be given data in the form of a 
comma-separated file or a Microsoft Excel file. You can read data from a file and also store output back into it using 
file handling.

Google Colab Images:

Files and Code:

Code:

Output Image:

Hello.txt