Class - 12 CBSE Computer Science Important File Handling Questions 2025
Write a method in python to read lines from a text file MYNOTES.TXT, and display those lines, which are starting with an alphabet 'K'.
Python File Handling
5 Likes
Answer
Let the file "MYNOTES.TXT" include the following sample text:
Kangaroo is a mammal native to Australia.
Lion is a large carnivorous.
Koala is an arboreal herbivorous.
Elephant is a large herbivorous mammal.
def display_lines(file_name):
with open(file_name, 'r') as file:
line = file.readline()
while line:
if line.strip().startswith('K'):
print(line.strip())
line = file.readline()
display_lines("MYNOTES.TXT")
Output
Kangaroo is a mammal native to Australia.
Koala is an arboreal herbivorous.
Answered By
3 Likes