Test Index
CBSE Class 12 Computer Science 2025 Paper Compartment
© examsnet.com
Question : 36 of 37
Marks:
+1,
-0
Keshav is the IT Head in a hospital. He needs to manage the records of all the doctors in the hospital. For this, he wants to store the following information of each doctor in a file :D_ID – An integer to store Doctor ID.D_Name – A string to store doctor's name.D_Dept – A string to store the Department of the doctor. (Surgery, Radiology, etc.)Experience – An integer to store doctor's experience (in years)For example, a doctor's information may be :[1256, 'R. Gupta', 'Cardiology', 15]As an applicant for the post of a Programmer, you have to answer the following questions in this context :(I) Write one difference of storing this data in a binary file over a CSV file.(II) Assume that the data is stored in a binary file, named DOCTORS.DAT, and each record is stored as a list. Write a function, in Python, to read and display all the records from the file DOCTORS.DAT.(III) Write a function addDoctor(), in Python, which accepts a doctor's data from the user and writes it in the file DOCTORS.DAT.
Solution:
Concept:A binary file stores Python objects with the pickle module: pickle.dump(obj, file) writes one object and pickle.load(file) reads the next one. pickle.load() raises EOFError when no record is left, so records are read in a while True loop inside try ... except EOFError.(I) Any one difference:• A binary file stores the data in the same form as in memory (pickled objects), so a record such as the list [1256, 'R. Gupta', 'Cardiology', 15] is read back as a list with 1256 and 15 still integers. A CSV file stores everything as plain text separated by commas, so every field is read back as a string and must be converted (e.g. int(row[0])).• A binary file is not human-readable (it cannot be opened and read in a text editor), so the records are not easily seen or changed by others; a CSV file is readable in any text editor or spreadsheet.(II) Open the file in read-binary mode 'rb' and load records one by one until EOFError:
import pickledef displayDoctors(): try: f = open("DOCTORS.DAT", "rb") # read-binary mode except FileNotFoundError: print("File not found.") return try: while True: rec = pickle.load(f) # reads one record (a list) print(rec) except EOFError: # raised at the end of the file f.close()(III) Open the file in append-binary mode 'ab' (so earlier records are not erased), accept the four fields with the right types (int for D_ID and Experience), form a list and dump it:import pickledef addDoctor(): f = open("DOCTORS.DAT", "ab") # append-binary: old records stay D_ID = int(input("Doctor ID: ")) D_Name = input("Doctor's name: ") D_Dept = input("Department: ") Experience = int(input("Experience (years): ")) rec = [D_ID, D_Name, D_Dept, Experience] pickle.dump(rec, f) # write the list to the file f.close() print("Record added.")Example run (addDoctor() called twice, then displayDoctors()):Doctor ID: 1256Doctor's name: R. GuptaDepartment: CardiologyExperience (years): 15Record added.Doctor ID: 1301Doctor's name: S. MehtaDepartment: RadiologyExperience (years): 8Record added.>>> displayDoctors()[1256, 'R. Gupta', 'Cardiology', 15][1301, 'S. Mehta', 'Radiology', 8]Key point: Use mode 'ab', not 'wb', in addDoctor(): 'wb' creates the file afresh and every earlier doctor record would be lost.
© examsnet.com
Go to Question: