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

Unit-IV - File Handling - Pickle Module

Uploaded by

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

Unit-IV - File Handling - Pickle Module

Uploaded by

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

 Serialization is a process of converting Python object or data

structures hierarchy into byte streams so that it can be written


into a file.
 A byte stream is, a stream of bytes – one byte is composed of
8 bits of zeros and ones.
 These byte streams can then be stored or transferred easily.
 Video games might be the most intuitive example of
serialization's usefulness.
 Serialization is the process of converting object state into a
format that can be transmitted or stored. The serialization
changes the object state into series of bits.
 Is the inverse of Pickling where a byte stream is converted into
an object hierarchy.
 Unpickling produces the exact replica of the original object.
 The object state could be reconstructed later in the opposite
process, called deserialization.
 Python’s module to achieve serialization/de-serialization.
 In order to work with the pickle module, you must first import
it in your program.
import pickle
 Then you may use dump() and load() methods to write and
read from an open binary file respectively.
 Import pickle module.
 Open binary file in the required mode.
 Process binary file by writing/reading objects using
appropriate methods.
 Once done, close the file
 A binary file is opened in the same way as any other file, but
make sure to use “b” with file modes to open a file in binary
mode.
 file=open(“stu.dat”,”wb”)
OR Notice ‘b’ is used with the file modes

 file=open(“stu.dat”,”rb”)

 file.close()
 In order to write an object on to a binary file, use dump()
function of pickle module.
 pickle.dump(<object-to-be-written>,<file handle-of-open-file>)
 In order to write an object on to a binary file, use load()
function of pickle module.
 <object>=pickle.load(<file handle-of-open-file>)

You might also like