KnowledgeBoat Logo

Class - 12 CBSE Computer Science Important File Handling Questions 2025

Write a program to search the names and addresses of persons having age more than 30 in the data list of persons stored in a text file.

Python Data Handling

2 Likes

Answer

Let the file "Persons.txt" include the following sample text:

Samyukta, Mumbai, 35
Anubhav, Chennai, 28
Aniket, Hyderabad, 42
Sarth, Bangalore, 31
f = open('Persons.txt', 'r')
lines = f.readlines()
for line in lines:
    data = line.strip().split(',')
    if len(data) >= 3 and int(data[2]) > 30:
        print('Name:', data[0], 'Address:', data[1])
f.close()        
Output
Name: Samyukta Address:  Mumbai
Name: Aniket Address:  Hyderabad
Name: Sarth Address:  Bangalore

Answered By

2 Likes