-1

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

  • 1
    Does this answer your question? [How to use append with pickle in python?](https://stackoverflow.com/questions/12761991/how-to-use-append-with-pickle-in-python) – slothrop Aug 22 '23 at 11:21
  • 1
    Note the top-voted answer there. If you appended to your pickle file when you saved, then you need repeated calls to `load` when you read it in. – slothrop Aug 22 '23 at 11:22
  • Did the file size not grow? – Kelly Bundy Aug 22 '23 at 12:05

0 Answers0