Class - 12 CBSE Computer Science Important File Handling Questions 2025
Write a function in Python to count and display the number of lines starting with alphabet 'A' present in a text file "LINES.TXT", e.g., the file "LINES.TXT" contains the following lines:
A boy is playing there.
There is a playground.
An aeroplane is in the sky.
A cricket match is being played.
The function should display the output as 3.
Python Data Handling
1 Like
Answer
The file "LINES.TXT" contains the following lines:
A boy is playing there.
There is a playground.
An aeroplane is in the sky.
A cricket match is being played.
def count_lines(file_name):
count = 0
with open(file_name, 'r') as file:
for line in file:
if line.strip().startswith('A'):
count += 1
print(count)
count_lines("LINES.TXT")
Output
3
Answered By
1 Like