Binary File Menu Driven
Binary File Menu Driven
def insert():
with open("a.dat","ab") as f1:
rec=[]
while True:
name=input("Enter name:")
roll=int(input("Enter roll:"))
data=[name,roll]
rec.append(data)
choice=input("Enter More:[y/n]:")
if(choice=='n'):
break
pickle.dump(rec,f1)
def show():
with open("a.dat","rb") as f2:
try:
while True:
rec=pickle.load(f2)
#print(rec)
for i in rec:
for j in i:
print(j,end="\t ")
print()
except EOFError:
pass
def search(r):
with open("a.dat","rb") as f2:
try:
while True:
rec=pickle.load(f2)
for i in rec:
if(i[1]==r):
print(i[0],"\t",i[1],end=" ")
#print()
except EOFError:
pass
def Update(R):
#rec=[]
found=False
with open("a.dat","rb+") as f3:
try:
while True:
rpos=f3.tell()
print(rpos)
rec=pickle.load(f3)
for row in rec:
if(row[1]==R):
row[0 ]=input("Enter the new name:")
row[1]=int(input("Enter new roll:"))
f3.seek(rpos)
pickle.dump(rec,f3)
found=True
except EOFError:
if(found==False):
print("Sorry,no matching record found:")
else:
print("Record Successfully updated:")
def Delete():
f1=open("a.dat","rb")
data=pickle.load(f1)
print(data)
f1.close()
r=int(input("Enter roll to be deleted:"))
f1=open("a.dat","wb")
try:
rec=[]
for i in data:
if(i[1]==r):
continue
rec.append(i)
pickle.dump(rec,f1)
except EOFError:
print(" ")
f1.close()
print("MENU:")
print("1.ADD:")
print("2.SHOW:")
print("3.SEARCH:")
print("4.UPDATE:")
print("5.DELETE:")
print("6.EXIT:")
print("7.INVALID CHOICE:")
while True: