Write a python function to count the number of lines starting with a digit in a text file diary.txt

Question

Program/Source Code

Here is source code of the Python Program to count the number of lines in a text file. The program output is also shown below.

fname = input("Enter file name: ")
num_lines = 0
with open(fname, 'r') as f:
    for line in f:
        num_lines += 1
print("Number of lines:")
print(num_lines)

Runtime Test Cases

  Case 1: Contents of file: Hello world   
Output: Enter file name: data1.txt 
Number of lines: 1   


Case 2: Contents of file: This programming language is Python   Output: Enter file name: data2.txt 
Number of lines: 2
0
Ajay Vardhan 6 months 0 Answers 323 views 0

Leave an answer

Browse
Browse