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.'''
Function in python to copy records from on binary file to another file
'''(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.'''
import pickle
def copyData():
f=open("SPORT.DAT","rb")
count=0
data=pickle.load(f)
a=[]
for i in data:
if i[0]=='Basket Ball':
a.append(i)
count+=1
fobj=open("BASKET.DAT","wb")
pickle.dump(a,fobj)
f.close()
fobj.close()
return count
copyData()
No comments:
Post a Comment
for more information please share like comment and subscribe