Binary File Handling Practice Questions


  1. A Binary file, CINEMA.DAT has the following structure:
    {MNO:[MNAME, MTYPE]}

    Where
    MNO – Movie Number
    MNAME – Movie Name
    MTYPE is Movie Type

    Write a user defined function, findType(mtype), that accepts mtype as parameter and displays all the records from the binary file CINEMA.DAT, that have the value of Movie Type as mtype.
  2. def findType(mtype):
        f=open("cinema.dat","rb")
        d=pickle.load(f)   #for eg s={1: ['DDLJ', 'suspense'], 2: ['gupt', 'thriller'], 3: ['bagban', 'family']}
        for k,v in d.items():
            if v[1]== mtype:
                print("Movie Number : {} Movie Name : {} Movie Type : {}".format(k,v[0],v[1]))
        f.close()
    
    movietype=input("Enter a type : ")
    findType(movietype)
    

  3. 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.
  4. coming soon

  5. Aman is a Python programmer. He has written a code and created a binary file record.dat with employeeid, ename and salary. The file contains 10 records. He now has to update a record based on the employee id entered by the user and update the salary. The updated record is then to be written in the file temp.dat. The records which are not to be updated also have to be written to the file temp.dat. If the employee id is not found, an appropriate message should to be displayed. As a Python expert, help him to complete the following code based on the requirement given above:
    
    import _______ #Statement 1
    
    def update_data():
           rec={}
           fin=open("record.dat","rb")
           fout=open("_____________") #Statement 2
           found=False
           eid=int(input("Enter employee id to update their salary :: "))
           while True:
                try:
                     rec=______________ #Statement 3
                     if rec["Employee id"]==eid:
                             found=True
                             rec["Salary"]=int(input("Enter new salary :: "))
                             pickle.____________ #Statement 4
                     else:
                             pickle.dump(rec,fout)
                 except:
                     break
        
           if found==True:
                 print("The salary of employee id ",eid," has been updated.")
           else:
                 print("No employee with such id is not found")
           fin.close()
           fout.close()
    
    (i) Which module should be imported in the program? (Statement 1)
    (ii) Write the correct statement required to open a temporary file named temp.dat. (Statement 2)
    (iii) Which statement should Aman fill in Statement 3 to read the data from the binary file, record.dat and in Statement 4 to write the updated data in the file, temp.dat?
  6. coming soon

  7. A binary file “STUDENT.DAT” has structure
    (admission_number, Name,Percentage)

    Write a function countrec() in Python that would read contents of the file “STUDENT.DAT” and display the details of those students whose percentage is above 75. Also display number of students scoring above 75%.
  8. coming soon

  9. A binary file “Book.dat” has structure :
    [BookNo, Book_Name, Author, Price]

    (i) Write a user defined function CreateFile() to input data for a record and add to Book.dat .
    (ii) Write a function CountRec(Author) in Python which accepts the Author name as parameter and count and return number of books by the given Author are stored in the binary file "Book.dat"
  10. coming soon