Home » All Blogs » Python » Python assignments » M.Sc Computer Application » M.Sc Python Programming Assignment 25
M.Sc Python Programming Assignments
Assignment 25
Question:
Write a Python program to count the number of lines in a text file
Code:
def count_file_lines(file_name):
line_count = 0
with open(file_name, 'r') as file:
for line in file:
line_count += 1
return line_count
# Example usage
file_name = input("Enter the file name: ")
line_count = count_file_lines(file_name)
print("Number of lines in the file:", line_count)
Output:
Enter the file name: goku
Number of lines in the file: 1