Class - 12 CBSE Computer Science Important File Handling Questions 2025
A file sports.dat contains information in following format:
Event - Participant
Write a function that would read contents from file sports.dat and creates a file named Atheletic.dat copying only those records from sports.dat where the event name is "Atheletics".
Python File Handling
14 Likes
Answer
Let the file "sports.dat" include the following sample records:
Athletics - Rahul
Swimming - Tanvi
Athletics - Akash
Cycling - Kabir
Athletics - Riya
def filter_records(input_file, output_file):
with open(input_file, 'r') as f_in:
with open(output_file, 'w') as f_out:
for line in f_in:
event, participant = line.strip().split(' - ')
if event == 'Athletics':
f_out.write(line)
filter_records('sports.dat', 'Athletic.dat')
The file "Atheletic.dat" includes following records:
Athletics - Rahul
Athletics - Akash
Athletics - Riya
Answered By
7 Likes