Tuesday, April 21, 2026

Python Binary File Program - Class XII ( CBSE) - Computer Science

import pickle
updatedList = []

# write some data to file
def write():
    with open('NewFile.dat','wb') as file1:
        d1 = [101,'ram',34]
        d2 = [102,'shyam',54]
        d3 = [103,'sita',88]
        pickle.dump(d1,file1)
        pickle.dump(d2,file1)
        pickle.dump(d3,file1)


# read the file
def read():
    with open('NewFile.dat','rb') as file2:
        try:
            while True:
                data=pickle.load(file2)
                print(data)
        except:
            file2.close()

def search():
    global updatedList
    with open('NewFile.dat','rb') as file:
        rno=int(input('enter the roll number to search:'))
        newMarks=int(input('enter new marks'))
        try:
            while True:
                data=pickle.load(file)
                if data[0]==rno:
                   data[2]=newMarks
                updatedList.append(data)
        except:
            file.close()

def update():
    global updatedList
    with open('NewFile.dat','wb') as file:
        for i in updatedList:
            pickle.dump(i,file)
    

#write()
read()
#search()
#update()
#read()

No comments:

Post a Comment