'''
Binary file -in this records of a file is a list. list is a collection of information.
to work with binary binary file we need to import module [Link] store and read the
information from the list.
.dat.
'''
import pickle
#1. way to add records in a binary file
f=open("[Link]","wb")
l=["anant is a student in science stream","akshit is a student of commerce stream"]
[Link](l,f) #dump ()is used to write the info in to the binary file
[Link]()
#--------------------------------------------------------------------------------------
---------
#2 way to add info into binary file
f=open("[Link]","ab")
l=[[1,"sheena",50],[2,"tina",80]]
[Link](l,f)
[Link]()
#-----------------------------------------------------------------
#3 way to add info into binary file
f=open("[Link]","ab")
n=input("enter ur name")
r=int(input("enter your rollno"))
l=[n,r]
[Link](l,f)
[Link]()
#------------------------------------------------------------------
#to display the info of binary file
f=open("[Link]","rb")
print([Link](f)) #load () is used to read the info of binary file.
[Link]()
import pickle
#TO CREATE A BINARY FILE BY USING LIST CONCEPT
def createb():
f=open("[Link]","ab")
l=["siddharth","Siddhant"]
[Link](l,f)
[Link]()
#READ THE BINARY FILE CONTENT
def readb():
f=open("[Link]","rb")
t=[Link](f)
r=[Link](f)
y=[Link](f)
print(t)
print(r)
print(y)
[Link]()
#--------------------------ANOTHER WAY TO READ THE CONTENT by using While loop
def readb():
f=open("[Link]","rb")
try:
while(f):#while True:
t=[Link](f)
print(t)
except:
pass
[Link]()
#--------- TO READ THE FILE CONTENT BY USING THE FOR LOOP
def readb():
f=open("[Link]","rb")
for i in range(3):
t=[Link](f)
print(t)
[Link]()
#-----------TO READ THE FILE CONTENT BY USING THE FOR LOOP
def readb():
f=open("[Link]","rb")
for i in range(3):
for J in [Link](f):
print(J)
[Link]()
createb() readb()
Binary file basic operations usig dictionary:
'''
#1. add records-------------------------------------------------------
import pickle as p
import os
f=open("[Link]","ab+")
while True:
r=int(input("enter rollno"))
n=input("enter name")
m=int(input("enter marks"))
s={"roll":r,"name":n,"marks":m}
[Link](s,f)
ch=input("enter more records(y/n)")
if(ch=='n'):
break
[Link](0)
while True:
try:
g=[Link](f)
print(g)
except EOFError:
break
[Link]()
#2 searching a record-------------------------------------------------------
x=False
f=open("[Link]","rb")
r=int(input("enter rollno to search"))
while True:
try:
d=[Link](f)
if r==d['roll']:
print("record found")
print("roll:",d['roll'],"name:",d['name'],"marks:",d['marks'])
x=True
except:
break
if(x==False):
print("record not found")
[Link]()
#3 delete a record-------------------------------------------------------
x=False
f=open("[Link]","rb")
f1=open("[Link]","wb")
r=int(input("enter rollno to delete"))
while True:
try:
d=[Link](f)
if r==d['roll']:
x=True
else:
[Link](d,f1)
except:
break
if(x==False):
print("record not found")
else:
print("record found and deleted")
[Link]()
[Link]()
[Link]("[Link]")
[Link]("[Link]","[Link]")
f2=open("[Link]","rb")
while True:
try:
g=[Link](f2)
print(g)
except EOFError:
break
[Link]()
#4 updation of a record-------------------------------------------------------
x=False
f=open("[Link]","rb")
f1=open("[Link]","wb")
r=int(input("enter rollno to update"))
while True:
try:
d=[Link](f)
if r==d['roll']:
d['roll']=int(input("enter new roll no"))
d['name']=input("enter new name")
d['marks']=int(input("enter new marks"))
[Link](d,f1)
x=True
else:
[Link](d,f1)
except:
break
if(x==False):
print("record not found")
else:
print("record found and updated")
[Link]()
[Link]()
[Link]("[Link]")
[Link]("[Link]","[Link]")
f2=open("[Link]","rb")
while True:
try:
g=[Link](f2)
print(g)
except EOFError:
break
[Link]()
#5 displaying all records-------------------------------------------------------
f3=open("[Link]","rb")
while True:
try:
g=[Link](f3)
print(g)
except EOFError:
break
[Link]()
'''
Write a definition for function Itemadd () to insert record into the binary file
[Link],
([Link]- id,gift,cost). info should stored in the form of dictionary.
'''
#Sol :
import pickle
def itemadd ():
f=open("[Link]","wb")
n=int(input("enter how many records"))
for i in range(n):
r=int(input('enter id'))
n=input("enter gift name")
p=float(input("enter cost"))
v={"id":r,"name":n,"cost":p}
[Link](v,f)
[Link]()
#-------------------------------------------------------------------------
'''
Write a definition for function SHOWINFO() to read each record of a binary file
[Link],
([Link]- id,gift,cost).Assume that info is stored in the form of dictionary
'''
#Sol:
import pickle
def SHOWINFO():
f=open("[Link]","rb")
while True:
try:
g=[Link](f)
print(g)
except:
break
[Link]()
#---------------------OR------------------
def SHOWINFO():
f=open("[Link]","rb")
while True:
try:
g=[Link](f)
print("item id",g['id'],"gift name",g['name'],"cost",g['cost'])
except:
break
[Link]()
#-------------------------------------------------------------------------
'''
Write a definition for function COSTLY() to read each record of a binary file
[Link], find and display those items, which are priced less than 50.
([Link]- id,gift,cost).Assume that info is stored in the form of dictionary
'''
#sol
def COSTLY():
f=open("[Link]","rb")
while True:
try:
r=[Link](f)
if(r['cost']<50):
print(r)
except:
break
[Link]()
#-----------------------------------------------------------------------------
'''
Write a definition for function COSTLY() to read each record of a binary file
[Link], find and display those items, which are priced more than 50.
([Link]- id,gift,cost).Assume that info is stored in the form of list
'''
#sol
def COSTLY():
f=open("[Link]","rb")
while True:
try:
r=[Link](f)
if(r['cost']>50):
print(r)
except:
break
[Link]()
#--------------------------------------------------------------------------
'''
Write a definition for function COSTLY() to read each record of a binary file
[Link], find and display those items, which are priced between 50 to 60.
([Link]- id,gift,cost).Assume that info is stored in the form of dictionary
'''
#sol
def COSTLY():
f=open("[Link]","rb")
while True:
try:
r=[Link](f)
if(r['cost']>=50 and r['cost']<=60):
print(r)
except:
break
[Link]()
#--------------------------------------------------------------------------
'''
Write a function for function to SEARCH()for a item from a binary file "[Link]"
The user should enter the itemno and function should search and
display the detail of items.([Link]- id,gift,cost).
Assume that info is stored in the form of dictionary
'''
import pickle
def SEARCH():
f=open("[Link]","rb")
n=int(input("enter itemno which u want to search"))
while True:
try:
r=[Link](f)
if(r['id']==n):
print(r)
except:
break
[Link]()
#---------------------------------------------------------------------------
'''
Write a function for function to SEARCH()for a item from a binary file "[Link]"
The user should enter the item name and function should search and
display the detail of items.([Link]- id,gift,cost).
Assume that info is stored in the form of dictionary
'''
import pickle
def SEARCH():
f=open("[Link]","rb")
n=input("enter gift name which u want to search")
while True:
try:
r=[Link](f)
if(r['gift']==n):
print(r)
except:
break
[Link]()
#--------------------------------------------------------------------------
'''
Write a function in to search and display details of all flights, whose
destination is “Mumbai†from a binary file “[Link]â€.
([Link]- fno,from (starting point), to (destination)).
Assume that info is stored in the form of dictionary
'''
import pickle
def FUN():
f=open("[Link]","rb")
while True:
try:
r=[Link](f)
if(r['to']=="Mumbai"):
print(r)
except:
break
[Link]()
#-------------------------------------------------------------------------
'''
write a function TOTALTeachers() to read each record of a binary file [Link],
find the total no of teachers, whose data is stored in the file and
display the same.([Link] contains : scode,sname,NOT (#no of teachers)).
Assume that info is stored in the form of dictionary
'''
import pickle
def TOTALTeachers():
f=open("[Link]","rb")
c=0
while True:
try:
r=[Link](f)
c=c+1
print(r)
except:
break
print(c)
[Link]()
#---------------------------------------------------------------------------
'''
Write a function in that would read the contents from the file [Link] and
creates a file named [Link] copying only those records from [Link] where
the game name is “BasketBallâ€.([Link] - gamename, participants).
Assume that info is stored in the form of dictionary
'''
import pickle
def fun():
f=open("[Link]","rb")
f1=open("[Link]","wb")
while True:
try:
r=[Link](f)
if(r['gamename']=="BasketBall"):
[Link](r,f1)
except:
break
print(c)
[Link]()
[Link]()
#------------------------------------------------------------------------
'''
Write a function in to read and display the detail of all the members whose
membership type is ‘L’ or ‘M’ from a binary file “[Link]â€.
([Link] contains the following info-
mno,type (l for life member M monthly member),mname).
Assume that info is stored in the form of dictionary
'''
import pickle
def fun():
f=open("[Link]","rb")
while True:
try:
r=[Link](f)
if(r['type']=="L" or r['type']=="M"):
print(r)
except:
break
print(c)
[Link]()
#--------------------------------------------------------------------------
'''
Write a definition for function DELETEINFO() from binary file
[Link]. The user should enter the itemno and function should search and
delete the entered itemno info
([Link]- id,gift,cost).
Assume that info is stored in the form of dictionary
'''
import pickle
import os
def DELETEINFO():
f=open("[Link]","rb")
f2=open("[Link]","wb")
a=int(input("enter item id which you want to delete"))
while True:
try:
r=[Link](f)
if(r['id']!=a):
[Link](r,f2)
except:
break
[Link]()
[Link]()
[Link]("[Link]")
[Link]("[Link]","[Link]")
#-----------------------------------------------------------------------------
'''
Write a definition for function DELETEINFO() from binary file
[Link]. The user should enter the item name and function should search and
delete the entered itemno info
([Link]- id,gift,cost).
Assume that info is stored in the form of dictionary
'''
import pickle
import os
def DELETEINFO():
f=open("[Link]","rb")
f2=open("[Link]","wb")
a=input("enter gift name which you want to delete")
while True:
try:
r=[Link](f)
if(r['gift']!=a):
[Link](r,f2)
except:
break
[Link]()
[Link]()
[Link]("[Link]")
[Link]("[Link]","[Link]")
#-----------------------------------------------------------------------------
'''
Write a definition for function UPDATEINFO() from binary file
[Link]. The user should enter the item name and function should search and
update the entered itemno info
([Link]- id,gift,cost).
Assume that info is stored in the form of dictionary
'''
import pickle
import os
def UPDATEINFO():
f=open("[Link]","rb")
f2=open("[Link]","wb")
s=[]
a=input("enter item name which we want to update")
while True:
try:
r=[Link](f)
if(r['gift']==a):
r['id']=int(input("enter new item id"))
r['name']=input("enter item name")
r['cost']=int(input("enter cost of item"))
[Link](r)
except:
break
[Link](s,f2)
[Link]()
[Link]()
[Link]("[Link]")
[Link]("[Link]","[Link]")
#---------------------------------------------------------------------
'''
Write a definition for function Itemadd () to insert record into the binary file
[Link],
([Link]- id,gift,cost). info should stored in the form of list.
'''
#Sol :
import pickle
def itemadd ():
f=open("[Link]","wb")
n=int(input("enter how many records"))
for i in range(n):
r=int(input('enter id'))
n=input("enter gift name")
p=float(input("enter cost"))
v=[r,n,p]
[Link](v,f)
[Link]()
#-------------------------------------------------------------------------
'''
Write a definition for function SHOWINFO() to read each record of a binary file
[Link],
([Link]- id,gift,cost).Assume that info is stored in the form of list
'''
#Sol:
import pickle
def SHOWINFO():
f=open("[Link]","rb")
while True:
try:
g=[Link](f)
print(g)
except:
break
[Link]()
#---------------------OR------------------
def SHOWINFO():
f=open("[Link]","rb")
while True:
try:
g=[Link](f)
print("item id",g[0],"gift name",g[1],"cost",g[2])
except:
break
[Link]()
#-------------------------------------------------------------------------
'''
Write a definition for function COSTLY() to read each record of a binary file
[Link], find and display those items, which are priced less than 50.
([Link]- id,gift,cost).Assume that info is stored in the form of list
'''
#sol
def COSTLY():
f=open("[Link]","rb")
while True:
try:
r=[Link](f)
if(r[2]<50):
print(r)
except:
break
[Link]()
#-----------------------------------------------------------------------------
'''
Write a definition for function COSTLY() to read each record of a binary file
[Link], find and display those items, which are priced more than 50.
([Link]- id,gift,cost).Assume that info is stored in the form of list
'''
#sol
def COSTLY():
f=open("[Link]","rb")
while True:
try:
r=[Link](f)
if(r[2]>50):
print(r)
except:
break
[Link]()
#--------------------------------------------------------------------------
'''
Write a definition for function COSTLY() to read each record of a binary file
[Link], find and display those items, which are priced between 50 to 60.
([Link]- id,gift,cost).Assume that info is stored in the form of list
'''
#sol
def COSTLY():
f=open("[Link]","rb")
while True:
try:
r=[Link](f)
if(r[2]>=50 and r[2]<=60):
print(r)
except:
break
[Link]()
#--------------------------------------------------------------------------
'''
Write a function for function to SEARCH()for a item from a binary file "[Link]"
The user should enter the itemno and function should search and
display the detail of items.([Link]- id,gift,cost).
Assume that info is stored in the form of list
'''
import pickle
def SEARCH():
f=open("[Link]","rb")
n=int(input("enter itemno which u want to search"))
while True:
try:
r=[Link](f)
if(r[0]==n):
print(r)
except:
break
[Link]()
#---------------------------------------------------------------------------
'''
Write a function for function to SEARCH()for a item from a binary file "[Link]"
The user should enter the item name and function should search and
display the detail of items.([Link]- id,gift,cost).
Assume that info is stored in the form of list
'''
import pickle
def SEARCH():
f=open("[Link]","rb")
n=input("enter itemno which u want to search")
while True:
try:
r=[Link](f)
if(r[1]==n):
print(r)
except:
break
[Link]()
#--------------------------------------------------------------------------
'''
Write a function in to search and display details of all flights, whose
destination is “Mumbai†from a binary file “[Link]â€.
([Link]- fno,from (starting point), to (destination)).
Assume that info is stored in the form of list
'''
import pickle
def FUN():
f=open("[Link]","rb")
while True:
try:
r=[Link](f)
if(r[2]=="Mumbai"):
print(r)
except:
break
[Link]()
#-------------------------------------------------------------------------
'''
write a function TOTALTeachers() to read each record of a binary file [Link],
find the total no of teachers, whose data is stored in the file and
display the same.([Link] contains : scode,sname,NOT (#no of teachers)).
Assume that info is stored in the form of list
'''
import pickle
def TOTALTeachers():
f=open("[Link]","rb")
c=0
while True:
try:
r=[Link](f)
c=c+1
print(r)
except:
break
print(c)
[Link]()
#---------------------------------------------------------------------------
'''
Write a function in that would read the contents from the file [Link] and
creates a file named [Link] copying only those records from [Link] where
the game name is “BasketBallâ€.([Link] - gamename, participants).
Assume that info is stored in the form of list
'''
import pickle
def fun():
f=open("[Link]","rb")
f1=open("[Link]","wb")
while True:
try:
r=[Link](f)
if(r[0]=="BasketBall"):
[Link](r,f1)
except:
break
print(c)
[Link]()
[Link]()
#------------------------------------------------------------------------
'''
Write a function in to read and display the detail of all the members whose
membership type is ‘L’ or ‘M’ from a binary file “[Link]â€.
([Link] contains the following info-
mno,type (l for life member M monthly member),mname).
Assume that info is stored in the form of list
'''
import pickle
def fun():
f=open("[Link]","rb")
while True:
try:
r=[Link](f)
if(r[1]=="L" or r[1]=="M"):
print(r)
except:
break
print(c)
[Link]()
#--------------------------------------------------------------------------
'''
Write a definition for function DELETEINFO() from binary file
[Link]. The user should enter the itemno and function should search and
delete the entered itemno info
([Link]- id,gift,cost).
Assume that info is stored in the form of list
'''
import pickle
import os
def DELETEINFO():
f=open("[Link]","rb")
f2=open("[Link]","wb")
a=int(input("enter item id which you want to delete"))
while True:
try:
r=[Link](f)
if(r[0]!=a):
[Link](r,f2)
except:
break
[Link]()
[Link]()
[Link]("[Link]")
[Link]("[Link]","[Link]")
#-----------------------------------------------------------------------------
'''
Write a definition for function DELETEINFO() from binary file
[Link]. The user should enter the item name and function should search and
delete the entered itemno info
([Link]- id,gift,cost).
Assume that info is stored in the form of list
'''
import pickle
import os
def DELETEINFO():
f=open("[Link]","rb")
f2=open("[Link]","wb")
a=input("enter item name which you want to delete")
while True:
try:
r=[Link](f)
if(r[1]!=a):
[Link](r,f2)
except:
break
[Link]()
[Link]()
[Link]("[Link]")
[Link]("[Link]","[Link]")
#-----------------------------------------------------------------------------
'''
Write a definition for function UPDATEINFO() from binary file
[Link]. The user should enter the item name and function should search and
update the entered itemno info
([Link]- id,gift,cost).
Assume that info is stored in the form of list
'''
import pickle
import os
def UPDATEINFO():
f=open("[Link]","rb")
f2=open("[Link]","wb")
s=[]
a=input("enter item name which we want to update")
while True:
try:
r=[Link](f)
if(r[1]==a):
r[0]=int(input("enter new item id"))
r[1]=input("enter item name")
r[2]=int(input("enter cost of item"))
[Link](r)
except:
break
[Link](s,f2)
[Link]()
[Link]()
[Link]("[Link]")
[Link]("[Link]","[Link]")
def SHOWINFO():
f=open("[Link]","rb")
while True:
try:
g=[Link](f)
print(g)
except:
break
[Link]()
UPDATEINFO()
SHOWINFO()
import pickle
def addr():
f=open("[Link]","wb")
n=int(input("enter how many records"))
for i in range(n):
r=int(input('enter rollno'))
n=input("enter name")
p=float(input("enter percentage"))
v=[r,n,p]
[Link](v,f)
[Link]()
def dispr():
f=open("[Link]","rb")
while True:
try:
g=[Link](f)
print(g)
except:
break
[Link]()
def searchb():
f=open("[Link]","rb")
a=int(input("enter rollno which we want to search"))
d=0
while True:
try:
r=[Link](f)
if(r[0]==a):
print(r)
except:
break
[Link]()
import os
def deleteb():
f=open("[Link]","rb")
f2=open("[Link]","wb")
a=int(input("enter rollno which we want to delete"))
d=0
while True:
try:
r=[Link](f)
if(r[0]!=a):
[Link](r,f2)
except:
break
[Link]()
[Link]()
[Link]("[Link]")
[Link]("[Link]","[Link]")
def updateb():
f=open("[Link]","rb")
f2=open("[Link]","wb")
s=[]
a=input("enter name which we want to update")
d=0
while True:
try:
r=[Link](f)
if(r[1]==a):
r[2]=94
[Link](r)
except:
break
[Link](s,f2)
[Link]()
[Link]()
[Link]("[Link]")
[Link]("[Link]","[Link]")
addr()
dispr()
deleteb()
updateb()
dispr()
#searchb()
import pickle
import os
tname = input('Enter name to delete :')
file1= open('[Link]','rb')
file2 = open('[Link]','wb')
while True:
try:
data = [Link](file1)
if tname !=data['name']:
[Link](data,file2)
except:
break
[Link]()
[Link]()
[Link]('[Link]')
[Link]('[Link]','[Link]')
print('Record deleted....')