0% found this document useful (0 votes)
3 views

Binary File Menu Driven

12th C.S. Binary files commands in python
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Binary File Menu Driven

12th C.S. Binary files commands in python
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import pickle

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:

choice=int(input("Enter Your choice:"))


print()
if(choice==1):
insert()
elif(choice==2):
show()
elif(choice==3):
rl=int(input("Enter the roll to be searched:"))
search(rl)
elif(choice==4):
rl=int(input("Enter the roll to be updated:"))
Update(rl)
elif(choice==5):
Delete()
elif(choice==6):
break
else:
print("Invalid Choice:")

You might also like