Add Headings and they will appear in your table of contents.
File : collection of information store on a secondary storage device where data are permanently with some name. We can read, write or edit data any time/
Binary File: store data in Binary form of bytes (0s and 1s), These files are not human readable. they are used to store image, audio, video, compressed files, executable files, etc. We cannot open a binary file using a text editor/ We need specific software to read or write the contents of a binary file. A small error in a text file can be recognized and eliminated when seen. Whereas, a small error in a binary file may corrupts the file and is not easy to detect./More secure then text file / Since the data is not human readable it also adds to the security of the content as one might not be able to get data if the structure is not known.
File Opening modes(12) : r,w,a, r+,w+,a+, rb,wb,ab, rb+,wb+,ab+ (default text file mode (r)) r+(read & write) / "a+" for append and read /
dump() method is used to write the object in binary file and load() method is used to read the object form file both methods are available in pickle module
Pickling(serialization, marshalling): Process where a python object is converted into a byte stream./Unpickling: Inverse of Pickling process where a byte stream is converted into a python object.
tell(): Tell the current position of file pointer for read write operation in bytes.
seek( ): Itmove the position of file pointer to the given position/It is used to change the position of file pointer for read write operation
1.Write dictionary into a binary file
import pickle
d={'python':90,'java':80,'C++':85}
f=open('abc.dat','wb')
pickle.dump(d,f)
f.close()
2. Read dictionary from a binary file
import pickle
f=open('abc.dat','rb')
dic=pickle.load(f)
f.close()
print(dic)
Writing List in binary file
import pickle
list=[1,2,3,4,5,6,7,8,9,10]
f=open('abc.dat','wb')
pickle.dump(list,f)
f.close()
Write employee name and salary into binary file
import pickle
f=open('emp.dat','wb')
n=int(input("Enter no of employees\n"))
d={ }
i=1
while i<=n:
name=input("Enter name")
salary=int(input("Enter salary"))
d[name]=salary
i=i+1
pickle.dump(d,f) # dump d into f
f.close()
Reading employ name and salary where salary greater than 4000 from binary file emp.dat
import pickle
f=open("emp.dat","rb")
rd=pickle.load(f)
for key,value in rd.items():
if(value>4000):
print(key,":",value)
f.close()
Create a binary file student.dat to hold students’ records like Rollno., Students name, and Address using the list. Write functions to write data, read them, and print on the screen.
import pickle
rec=[]
def create():
f=open("student.dat","wb")
rno = int(input("Enter Student No:"))
sname = input("Enter Student Name:")
address = input("Enter Address:")
rec=[rno,sname,address]
pickle.dump(rec,f)
def read():
f = open("student.dat","rb")
print("Data stored in File....")
rec=pickle.load(f)
for i in rec:
print(i)
create()
read()
Write a program to store customer data into a binary file cust.dat using a dictionary and print them on screen after reading them. The customer data contains ID as key, and name, city as values.
import pickle
def bin2dict():
f = open("cust.dat","wb")
d = {'C0001':['Subham','Ahmedabad'],
'C0002':['Bhavin','Anand'],
'C0003':['Chintu','Baroda']}
pickle.dump(d,f)
f.close()
f = open("cust.dat","rb")
d = pickle.load(f)
print(d)
f.close()
bin2dict()
Write a function searchprod( pc) in python to display the record of a particular product from a file product.dat whose code is passed as an argument. Structure of product contains the following elements [product code , product price]
import pickle
def searchprod(pc):
f=open("product.dat","rb")
num=0
while True:
rec=pickle.load(f)
if rec[0]==pc:
print(rec)
f.close()
n=searchprod(1)
Write a function countrec(sport name) in Python which accepts the name of sport as parameter and count and display the coach name of a sport which is passed as argument from the binary file “sport.dat”. Structure of record in a file is given below ——————– – [sport name, coach name]
def countrec(sn):
num=0
f=open("data.dat","rb")
print("Sport Name","\t","Coach Name")
while True:
rec=pickle.load(f)
if rec[0]==sn:
print(rec[0],"\t\t",rec[1])
num=num+1
return num
f.close()