Delete a record from binary file using employee code
import pickle import os #function definition def delete_empno(): try: z=0 tr=int(input("Enter Empno to delete ")) f=open("emp","rb") tf=open("temp","wb") print("Empno","Name","Salary") while True: rec=pickle.load(f) if rec[0]==tr: z=1 print(rec[0],rec[1],rec[2]) else: pickle.dump(rec,tf) except EOFError: f.close() tf.close() if z==0: print("Record not found") else: os.remove("emp") os.rename("temp","emp") except IOError: print("Unable to open the file") #function calling delete_empno()
Update a record from binary file using employee code
import pickle import os #function definition def update_empno(): try: z=0 tr=int(input("Enter Empno to update ")) f=open("emp","rb") tf=open("temp","wb") print("Empno","Name","Salary") while True: rec=pickle.load(f) if rec[0]==tr: z=1 print("Old Record") print(rec[0],rec[1],rec[2]) print("Enter new data ") empno=int(input("Enter Empno ")) name=input("Enter name ") name=name.upper() sal=float(input("Enter Salary ")) rec=[empno,name,sal] pickle.dump(rec,tf) except EOFError: f.close() tf.close() if z==0: print("Record not found") else: os.remove("emp") os.rename("temp","emp") except IOError: print("Unable to open the file") #function calling update_empno()