English

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

Advertisements
Advertisements

Question

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.

Short/Brief Note
Advertisements

Solution

def copyData():
                    fObj = open("SPORT.DAT", "rb")
                    fObj1 = open ("BASKET.DAT", "wb")
                    cnt=0
                    try:
                           while True:
                                 data = pickle.load(fObj)
                                 print (data)
                                 if data[0] == "Basket Ball":
                                         pickle.dump (data, fObj1)
                                         cnt+=1
except:
          fObj.close()
          fObj1.close()
return cnt
shaalaa.com
The Pickle Module in Python
  Is there an error in this question or solution?
2023-2024 (March) Board Sample Paper
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×