Q. 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".


You can understand by Watching video :-



Answer :-

Binary File Program :-

import pickle
def  portal(  ) :
    file1 = open("sports.dat","rb")
    file2 = open("Atheletics.dat","wb")
    try :
        while True :
            data = pickle.load( file1 )
            word = data.split(" ~ ")
            if data [ : 9 ] == "atheletic" or data [ : 9 ] == "Atheletic":
                pickle.dump( data, file2 )
    except EOFError :
        file1.close()
        file2.close()
    print("Pragram Run Succesfully !!")
portal()

Text File Program :-

def  portal(  ) :
    file1 = open("sports.txt","r")
    file2 = open("Atheletics.txt","w")
    lst = file1.readlines()
    for i in lst :
        print(i [ : 9 ])
        if i [ : 9 ] == "atheletic" or i [ : 9 ] == "Atheletic" :
            file2.write(i)
            
    file1.close()
    file2.close()
    
portal()

We write Data in sports.dat file by following script

import pickle
f = open( "sports.dat","wb" )
pickle.dump("Football ~ Path",f)
pickle.dump("Atheletics ~ Walla",f)
pickle.dump("Cricket ~ Portal",f)
pickle.dump("Hockey ~ Express",f)
pickle.dump("Atheletics ~ Python",f)
pickle.dump("Chess ~ Computer",f)
f.close()

sports.dat file contain :- ( in Binary Language)

€•       Å’Football ~ Path”.€•       Å’Atheletics ~ Walla”.€•       Å’Cricket ~ Portal”.€•       Å’Hockey ~ Express”.€•       Å’Atheletics ~ Python”.€•       Å’Chess ~ Computer”.

Output :-

Program Run Successfully !!
>>> 

Atheletic.dat file Contain :- ( in Binary Language )

€•       Å’Atheletics ~ Walla”.€•       Å’Atheletics ~ Python”.

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post