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

Binary File Programs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Binary File Programs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

#Write a program to insert, display and search a number in a binary file.

If
number is not found then display appropriate message.
#single list program
import pickle

def write():
f=open("sample.dat",'wb')
rec=[3,5,7,2,6,7,1] #pre-defined list
#user input code below
'''
while True:
x=int(input("enter value:"))
rec.append(x)
ch=input("enter more?:")
if(ch=='n'):
break
'''
pickle.dump(rec,f) # entire list written to file once
print("added")
f.close()

def read():
print("reading...")
f=open("sample.dat",'rb')
p=pickle.load(f)
print(p)
f.close()

def search():
print("searching....")
f=open("sample.dat",'rb')
found=0
x=int(input("enter the number you want to search:"))
rec=pickle.load(f)
print(rec)
for R in rec:
if x==R:
found=1
print("record found")
break
if(found==0):
print("record not found")
f.close()

print("*"*20,"MENU","*"*20)
print("1.write\n2.read\n3.search")
choice=int(input("enter your choice:"))
if(choice==1):
write()
elif choice==2:
read()
elif choice==3:
search()
else:
print("invalid input")

-------------------------------------------------------------------------------
#Write a program to search a record using its roll number and display the name
of student. If record not found then display appropriate message.
#nested list

import pickle

def write():
f=open("sample.dat",'wb')
rec=[[1,'sakshi',45],[2,'kiara',66],[3,'inaya',47],[4,'anjali',76]] #pre-set list
pickle.dump(rec,f)
print("added")
f.close()

def read():
print("reading...")
f=open("sample.dat",'rb')
p=pickle.load(f)
#print(p) # will print the nested list
#display element by element
print("rno name marks")
for i in p:
print(" ",i[0],i[1]," ",i[2])
f.close()

def search():
print("searching....")
f=open("sample.dat",'rb')
found=0
x=int(input("enter the rno you want to search:"))
rec=pickle.load(f)
#print(rec) #... will print nested list
for i in rec:
if x==i[0]:
found=1
print("record found")
print("name=",i[1],"marks=",i[2])
break
if(found==0): print("record not found")
f.close()

print("*"*20,"MENU","*"*20)
print("1.write\n2.read\n3.search")
choice=int(input("enter your choice:"))
if(choice==1):
write()
elif choice==2:
read()
elif choice==3:
search()
else:
print("invalid input")
----------------------------------------------------------------------------------------------------
#Write a program to write and display the contents of a dictionary into a binary
file.
#only dictionary but multiple records

import pickle
def write():
f=open("maps.dat","ab")
ch="y"
while ch == "y":
r=int(input("input enter rno "))
s=input("enter name ")
m=int(input("input enter marks "))
d={"rno":r,"name":s,"MARKS":m}
pickle.dump(d,f) # only 1 record dumped in file.loop repeats, next
dumped.
#
dictionaries dont have append() function, so each record needs to be written
separately #using loop
ch=input("want to enter more rec ")
f.close()

def read():
f=open("maps.dat","rb")
d={}
try:
while True: # as long as there is data in the file
d=pickle.load(f)
print(d['rno'],':',d['name'])
#print(d)
except EOFError:
f.close()

write()
read()

-------------------------------------------------------------------
#WAP to read,write and seach a record in a binary file using list of dictionaries.
#binary file - list of dictionary

import pickle
def writedata( ):
list =[ ]
while True:
roll = input("Enter student Roll No:")
sname = input("Enter student Name :")
student = {"roll":roll,"name":sname}
list.append(student)
choice= input("Want to add more record(y/n) :")
if(choice=='n'):
break
file = open("student.dat","wb")
pickle.dump(list,file) #list of dictionary dumped only once in file as
now dictionary is appended inside the list
file.close( )

def readdata( ):
file = open("student.dat", "rb")
list1 = pickle.load(file)
print(list1)
for i in list1:
print(i['roll'],i['name'])
file.close()

def search():
file = open("student.dat", "rb")
list1 = pickle.load(file)
pname=input("enter the name to be searched:")
for i in list1:
if (i['name']==pname):
print("record found!")
print("rollno=",i['roll'],"name=",i['name'])
break
else:
print("not found!")

print("Press-1 to write data and Press-2 to read data Press 3 to search record ")
choice=int(input( "enter choice:"))
if choice==1:
writedata()
elif choice==2:
readdata()
elif choice==3:
search()
else:
print("You entered invalid value")

You might also like