A global bank manages its customer in a binary data file “Bank.DAT” with the following fields:
• Account number
• Account holder’s name
• Account balance
Give function definitions to do the following:
a) Write a function to accept the data fields from the user and write to file “Bank.DAT”.
b) Write a function to modify account holder details according to their account number and display the updation.
Answers
Language:
Python
Program:
a)
def createFile():
import pickle
f=open('Bank.dat','wb')
record=[]
while True:
accno=input("Enter account number:")
holder=input(f"Enter {accno} holder's name: ")
balance=float(input(f"Enter {accno}'s balance: "))
choice=input("Wanna enter more (Y/N)?: ")
record.append([accno, holder, balance])
if choice=='N':
break
pickle.dump(record,f)
print(len(record),'record(s) inserted')
f.close()
b)
def update():
import pickle
f=open('Bank.dat','rb+')
read=pickle.load(f)
found=0
acc=input('Enter account number to update: ')
for i in read:
if i[0]==acc:
print("Make the following choice for updation: \nHolder's name\t1 \nAccount Balance\t2 \nCancel updation\t3")
choice=int(input("Enter your choice: "))
found+=1
if choice==1:
i[1]=input('Enter new Holder\'s name: ')
break
elif choice==2:
i[2]=int(input("Enter new Balance:"))
break
elif choice==3:
print("Aborting updation.")
else:
print("Invalid choice")
break
else:
found+=0
if found==1:
f.seek(0)
pickle.dump(read,f)
print("Done.")
else:
print("Record not found.")
f.close()
Explanation:
- def defines function and accepts input though while loop and loads them into the binary file after creating it using pickle module.
- In the second function option is provided to update the data and update is though rb+ mode with seek, dump function if the file had that specific record.