Thursday, 29 May 2025

GRADE 12 - PRACTICAL 11 - BINARY FILES

"""WAPP to demonstrate the following optionsby creating different methods using binary files for a hospital management application :

(i) adding records of the structure :  {'pid':<str>,'pname':<str>,'diag':<str>,'fees':<int>}

(ii)reading and displaying all the records in a

tabular format

(iii)searching and displaying patients who have 'corona'

(iv)update the diagnosis of the given patient id

(Display appropriate messages if patient not found or file not found.)

"""



import pickle def menu(): while True: print("""Menu 1)Enter patient details 2)Display patient details 3)Search 4)Update 5)Exit'""") ch = int(input("Enter your choice : ")) if ch==1: accept() elif ch==2: display() elif ch==3: search() elif ch==4: update() else: break def accept(): with open('Diagnostics.dat','ab') as f: pid=input('Enter patient id : ') pname=input(("Enter patient's name : ")) diag = input('Enter patient diagnosis : ') fee = int(input('Enter patient fee :')) d={'pid':pid,'pname':pname,'diag':diag,'fee':fee} pickle.dump(d,f) def display(): try: with open('Diagnostics.dat','rb') as f: print('pid'.ljust(15),'pname'.ljust(20),'diagnosis'.ljust(15),'fees') while True: rec = pickle.load(f) print(rec['pid'].ljust(15),rec['pname'].ljust(20),rec['diag'].ljust(15),rec['fees']) except FileNotFoundError: print('Sorry, File not found') except EOFError: pass except Exception as e: print("Unexpected error : ",e) def search(): try: with open('Diagnostics.dat','rb') as f: print("Patient details with corona : ") while True: rec = pickle.load(f) if rec[2] == "corona": for i in rec: print(str(i).ljust(15),end='') print() except FileNotFoundError: print('Sorry, File not found') except EOFError: pass except Exception as e: print("Unexpected error : ",e) def update(): try: with open('Diagnostics.dat','rb') as f: pid=input("Enter pid of patient to be updated : ") l=[] found=False while True: rec = pickle.load(f) if rec[0] == pid: rec[2]=input("Enter new diagnosis") found=True print("Record updated successfully") l.append(rec) if found== False: print("Required record not found") with open('Diagnostics.dat','wb') as f: for rec in l: pickle.dump(rec,f) except FileNotFoundError: print('Sorry, File not found') except EOFError: pass except Exception as e: print("Unexpected error : ",e) menu()

No comments:

Post a Comment