Programming Examples

Python program to create a function that would read contents from the sports.dat and creates a file named Atheletic.dat


A file sports.dat contains information in the following format : Event ~ Participant

Write a function that would read contents from the sports.dat and creates a file named Atheletic.dat copying only those records from sports.dat where the event name is "Atheletics".

Solution

def  filter(  ) :
    file1 = open("sports.dat","r")
    file2 = open("Atheletics.dat","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()    
filter()
Output

CCC Online Test Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Online Exam Quiz O Level NIELIT Study material and Quiz Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Best Website and Software Company in Allahabad Website development Company in Allahabad