Friday, 23 July 2021

read, write/create, search, append, update & delete methods in Binary file 

 

Program




































































Output






































































program

#Menu Driven program to perform all the operations

#on a Binary file....

import pickle

def write():

    f=open("StudentDetails.dat","wb")

    record=[]

    while True:

        rno=int(input("Roll No:"))

        name=input("Name:")

        marks=int(input("Marks:"))

        data=[rno,name,marks]

        record.append(data)

        ch=input("More(Y/N)?")

        if ch in 'Nn':

            break

    pickle.dump(record,f)

    print("Record Added..")

    f.close()


def read():

    print("Contents in a File...")

    f=open("StudentDetails.dat","rb")

    try:

        while True:

            s=pickle.load(f)

            print(s)

            for i in s:

                print(i)

    except Exception:

        f.close()


def append():

    f=open("StudentDetails.dat","rb+")

    print("Append data in a File..")

    Rec=pickle.load(f)

    while True:

        rno=int(input("Roll No:"))

        name=input("Name:")

        marks=int(input("Marks:"))

        data=[rno,name,marks]

        Rec.append(data)

        ch=input("More(Y/N)?")

        if ch in 'Nn':

            break

    f.seek(0)

    pickle.dump(Rec,f)

    print("Record Appended..")

    f.close()

            

def search():

    f=open("StudentDetails.dat","rb")

    r=int(input("Enter roll no to be searched:"))

    found=0

    try:

        while True:

            s=pickle.load(f)

            for i in s:

                if i[0]==r:

                    print(i)

                    found=1

                    break

    except EOFError:

        f.close()

    if found==0:

        print("Sorry...No record Found..")


def update():

    f=open("StudentDetails.dat","rb+")

    r=int(input("Enter roll no whose details to be updated:"))

    f.seek(0)

    try:

        while True:

            rpos=f.tell()

            s=pickle.load(f)

            print(s)

            for i in s:

                if i[0]==r:

                    i[1]=input("New Name:")

                    i[2]=int(input("Updated Marks:"))

                    f.seek(rpos)

                    pickle.dump(s,f)

                    break

    except Exception:

        f.close()

                

def delete():

    f=open("StudentDetails.dat","rb")

    s=pickle.load(f)

    f.close()

    r=int(input("Enter roll no to be deleted:"))

    f=open("StudentDetails.dat","wb")

    print(s)

    reclst=[]

    for i in s:

        if i[0]==r:

            continue

        reclst.append(i)

    pickle.dump(reclst,f)

    f.close()

        

#MainMenu

print("---------------------------------------------------")

print("1.Write Data in a Binary File..")

print("2.Read Data from a File..")

print("3.Append data in a File..")

print("4.Search Operation in a File..")

print("5.Update Data in a File..")

print("6.Delete Operation in a File..")

print("7.Exit..")

print("---------------------------------------------------")


while True:

    ch=int(input("Enter your choice:"))

    if ch==1:

        write()

    elif ch==2:

        read()

    elif ch==3:

        append()

    elif ch==4:

        search()

    elif ch==5:

        update()

    elif ch==6:

        delete()

    elif ch==7:

        break

                   

    



No comments:

Post a Comment