BINARY FILES-1
"Binary" files are any files where the format
isn't made up of readable characters.
Binary files can range from image files like
WHAT JPEGs or GIFs or audio files or word files etc
ARE Files will open in appropriate softwares like
photoshop, word etc
BINARY
FILES? Data type is preserved in binary file
There are not EOF characters
Deepshikha Sethi -----XII Computer Science ----AIS Mayur Vihar
To open a file in binary mode
“b” is added to the mode
Creating f=open("file.txt","wb")
Binary
Files in f=open('file1.dat','rb')
Python
f=open('file1.dat', 'ab+')
Deepshikha Sethi -----XII Computer Science ----AIS Mayur Vihar
File extension can be .txt as well as .dat
Writing into Binary files (Pickling)
To be note down in copy
• Pickle module is used to handle binary files
import pickle
f=open("file1.dat","wb")
d={1:"apple",2:"mango"}
pickle.dump(d,f) Dump writes the object d in file object f
Deepshikha Sethi -----XII Computer Science ----AIS Mayur Vihar
Writing into Binary files
To be note down in copy
Writing Multiple type of data into binary files
import pickle
f=open("file2.dat","wb")
a="New thing" # string
l=[10,20,30,40] #List
d={1:"apple",2:"mango"} #dictionary
pickle.dump(a,f)
pickle.dump(l,f)
pickle.dump(d,f)
Deepshikha Sethi -----XII Computer Science ----AIS Mayur Vihar
f.close()
Reading data from a binary file (Unpickling)
To be note down in copy
• Simple reading Output
import pickle New thing <class 'str'>
f=open("file2.dat","rb") [10, 20, 30, 40] <class 'list'>
m=pickle.load(f) {1: 'apple', 2: 'mango'} <class 'dict'>
print(m,type(m))
n=pickle.load(f) Load method returns object data from the pickled file
print(n,type(n))
o=pickle.load(f)
print(o,type(o))
Deepshikha Sethi -----XII Computer Science ----AIS Mayur Vihar
How to read data from binary file where no of records are not known
To be note down in copy
import pickle
f=open("file2.dat","rb")
while(True): while(True) - Infinite loop
try: Exception handled
obj=pickle.load(f) EOFError
print(obj)
except EOFError: Output
New thing
f.close()
Deepshikha Sethi -----XII Computer Science ----AIS Mayur Vihar
[10, 20, 30, 40]
{1: 'apple', 2: 'mango'}
break
Serialization
The pickle module is used for implementing binary protocols
for serializing and de-serializing a Python object structure.
• “Pickling” is the process whereby a Python object hierarchy is
converted into a byte stream
• “Unpickling” is the inverse operation, whereby a byte stream
(from a binary fileor bytes-like object)
Deepshikha Sethi -----XII Computer Science ----AIS Mayur Vihar
Application of pickle module
Task
1) UDF Save dictionary object containing {RNO, Name, Marks} to a pickled
file student.dat
2) UDF Read all objects from file student.dat
Deepshikha Sethi -----XII Computer Science ----AIS Mayur Vihar
1) UDF Save dictionary object containing {RNO,
Name, Marks} to a pickled file student.dat
import pickle def readfile():
def writefile(): f=open("student.dat","rb")
fobj=open("student.dat","ab") while True:
d["rno"]=int(input('Enter the rno')) try:
d["name"]=input('Enter the name') obj=pickle.load(f)
d["marks"]=int(input('Enter the marks')) print(obj)
pickle.dump(d,fobj) except EOFError:
fobj.close() break
f.close()
Deepshikha Sethi -----XII Computer Science ----AIS Mayur Vihar
1) UDF Read all objects from file student.dat
def menu():
while(True):
print("1: Write data")
print("2: Read data")
print("3: Exit")
ch=int(input('Enter the choice'))
if(ch==1):
writefile()
elif(ch==2): Binary-5.py
readfile()
elif(ch==3):
break
menu()
Deepshikha Sethi -----XII Computer Science ----AIS Mayur Vihar
Recap
1) The process of pickling in Python includes:
A) conversion of list into a database
B) conversion of byte stream into object hierarchy
C) conversion of a Python object hierarchy into byte stream
D)conversion of a datatable into a list
Deepshikha Sethi -----XII Computer Science ----AIS Mayur Vihar
Recap
2) The method _____________ allows serialize a data stream
3) Differentiate between write() and dump() methods?
4) Differentiate between load() and read() methods?
Deepshikha Sethi -----XII Computer Science ----AIS Mayur Vihar