So I am writing Python code for my Homework(I have to use Classes), And the problem is that it refuses to append data, It works fine for the first entry of data and then it doesn't do anything
import pickle
class DataBase:
def addRec(self,name,roll,Class,percentage):
self.name = name
self.Roll_No = roll
self.Class = Class
self.percentage = percentage
f = open('School.dat', 'ab')
lst = [self.name , self.Roll_No, self.Class, self.percentage]
pickle.dump(lst, f)
def readRec(self):
with open('School.dat', 'rb') as f:
rr = pickle.load(f)
print(rr)
School = DataBase()
while True:
ch = int(input('Enter choice '))
if ch == 1 :
name = input('Enter Name :')
Roll = (input('Enter Roll :'))
Class = input('Enter Class :')
Percentage = (input('Enter Percentage :'))
School.addRec(name, Roll, Class, Percentage)
if ch == 2:
School.readRec()
if ch == 3:
break
I have tried changing the append mode to write mode and now it accepts new data but it deletes previous data FOUND THE PROBLEM, I WAS NOT USING TRY AND EXCEPT BLOCKS , THANKS TO SLOTHROP AND KELLY BUNDY