(ii) Consider a file, SPORT.DAT, containing records of the following structure:

[SportName, TeamName, No_Players]

Write a function, copyData(), that reads contents from the file SPORT.DAT and copies the records with Sport name as “Basket Ball” to the file named BASKET.DAT. The function should return the total number of records copied to the file BASKET.DAT.

Answer

Answer by student

Consider a file, SPORT.DAT, containing records of the following - CBSE Class 12 Sample Paper for 2024 Boards

Detailed answer by teachoo

-lock-

The question asks us to write a function, copyData(), that reads contents from the file SPORT.DAT and copies the records with Sport name as “Basket Ball” to the file named BASKET.DAT. The function should return the total number of records copied to the file BASKET.DAT.

To do this, we need to use the following steps:

  • We start by defining a function named copyData using the def keyword. A function is a block of code that can be reused and executed by calling its name. A function can have parameters and return values.
  • We then open the file SPORT.DAT in read mode using the built-in open function. This will create a file object that we can use to read data from the file. We assign this object to a variable named sport_file. 
  • We then open the file BASKET.DAT in write mode using the open function. This will create another file object that we can use to write data to the file. We assign this object to another variable named basket_file. The mode “w” means write mode , which allows us to write data to the file only.
  • We then initialize a variable named count to store the number of records copied from SPORT.DAT to BASKET.DAT . We set this variable to 0 initially using the assignment operator (=). 
  • We then loop through each line in sport_file using a for loop. Each iteration will assign one line from the file to a variable named line. The line will include a newline character (\n) at the end. The file pointer will move to the next line after each iteration.
  • We then split the line by comma and assign the resulting list to fields . This will use the split method of string objects. This will create a list of three elements: [SportName, TeamName, No_Players]. The split method takes a delimiter as an argument and returns a list of substrings that are separated by that delimiter. In this case, we want to split by comma, so we pass “,” as an argument.
  • We then check if the first element of fields is equal to “Basket Ball” using an if statement. If they are equal, the condition will be True and the following block of code will be executed.
  • We then write the line to basket_file using the write method. This will update the file pointer to point after the written line.
  • We then increment count by 1 using the += operator. This will update count with each record copied from SPORT.DAT to BASKET.DAT.
  • We then close both files using the close method.  
  • We then return count using the return statement. 

So, the code for function copyData() is:

part 2 - Question 32 (ii) (Choice 1) - CBSE Class 12 Sample Paper for 2024 Boards - Solutions to CBSE Sample Paper - Computer Science Class 12 - Computer Science - Class 12

-endlock-

Remove Ads Share on WhatsApp
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh is an IIT Kanpur graduate and has been teaching for 16+ years. At Teachoo, he breaks down Maths, Science and Computer Science into simple steps so students understand concepts deeply and score with confidence.

Many students prefer Teachoo Black for a smooth, ad-free learning experience.