Binary Files - File Handling
Binary Files - File Handling
To save any object structure along with data, Python provides a module called Pickle. The
module Pickle is used for serializing and de-serializing any Python object structure.
Pickling is a method of preserving food items by placing them in some solution, which
increases the shelf life. In other words, it is a method to store food items for later
consumption. Serialization is the process of transforming data or an object in memory (RAM)
to a stream of bytes called byte streams.
These byte streams in a binary file can then be stored in a disk or in a database or sent
through a network. Serialization process is also called pickling.
De-serialization or unpickling is the inverse of pickling process where a byte stream is
converted back to Python object. The pickle module deals with binary files. Here, data are
not written but dumped and similarly, data are not read but loaded.
The Pickle Module must be imported to load and dump data. The pickle module provides
two methods - dump() and load() to work with binary files for pickling and unpickling,
respectively.
PICKELINGAND UNPICKLING USING
PICKEL MODULE
dump(object,fileobject)
pickle.dump() Method
Example: A program to write list sequence in a binary file
pickle.dump() Method
OUTPUT
OUTPUT
File Handling
Binary file R/W Operation
import pickle
using pickle module
output_file = open("a.bin", "wb") In this program we open a.bin file in
myint = 42
mystring = "Python!" binary mode using ‘wb’ specification
mylist = ["python", "sql", "mysql"] and create and store value in 4
mydict = { "name": "ABC", "job": "XYZ" }
pickle.dump(myint, output_file) different data objects i.e.
pickle.dump(mystring, output_file) int,string,list,dict.Write these into
pickle.dump(mylist, output_file)
pickle.dump(mydict, output_file) binary file using pickle.dump()
output_file.close() method then close this file and open
input_file = open("a.bin", "rb")
myint = pickle.load(input_file) the same for reading operation.We
mystring = pickle.load(input_file) read the content using load method()
mylist = pickle.load(input_file)
mydict = pickle.load(input_file) and display the value read from file.
print("myint = %s" % myint) To use dump and load we have to
print("mystring = %s" % mystring)
print("mylist = %s" % mylist) import pickle module first of all.
print("mydict = %s" % mydict)
input_file.close()
File Handling
Writing into Binary Files
Example: Write dictionary data to a Binary File:
import pickle
e={'Namita':25000,'Manya':30000,'Tanu':20000}
f1=open('emp.dat','wb')
pickle.dump(e,f1)
f1.close()
File Handling
Iteration over
import pickle Binary file - pickle module
output_file = open("a.bin", "wb")
myint = 42
mystring = "Python.mykvs.in!"
mylist = ["python", "sql", "mysql"]
mydict = { "name": "ABC", "job": "XYZ" }
pickle.dump(myint, output_file)
pickle.dump(mystring, output_file)
pickle.dump(mylist, output_file)
pickle.dump(mydict, output_file)
output_file.close()
with open("a.bin", "rb") as f:
while True:
try:
r=pickle.load(f) Read objects
print(r)
one by one
print(“Next data")
except EOFError:
break
f.close()
File Handling
Insert/append record (using
Dictionaries) in a Binary file
- pickle module
rollno = int(input('Enter roll number:'))
name = input('Enter Name:')
marks = int(input('Enter Marks'))
seek method
tell method
seek method
fileobject.seek(offset [, from_what])
here offset is used to calculate the position
of fileobject in the file in bytes. Offset is added to
from_what (reference point) to get the position.
Following is the list of from_what values:
seek method
and so on..
tell method
tell() method returns an integer giving
the current position of object in the file. The
integer returned specifies the number of bytes
from the beginning of the file till the current
position of file object.
It's syntax is
fileobject.tell()
tell() method
tell()method