As for now, you have learned to write lines/strings and lists on files.
Sometimes we may need to write and read non-simple objects like
dictionaries, tuples, lists, or nested lists, and so hence on to the files.
Since objects have some structure or hierarchy associated,
it remains important that they do serialize and then stored in binary files.
Serialization
Serialization which can also call Pickling is the process of converting Python object hierarchy,
into a byte stream so that it can be written into a file.
Pickling converts an object into a bytes stream in such a method that it can denote reconstruction in original form when unpickled or de-serialized.
De-Serialization
Unpickling is the opposite of Pickling where a byte stream is transformed into an object hierarchy.
Unpickling produces an accurate copy of the original object.
Python provides the pickle module to achieve this. As per Python’s documentation,
“The pickle module performs a fundamental, but powerful algorithm for serializing and de-serializing a Python object structure.”
In this part, we shall learn how to use the pickle module for reading/writing objects in binary files.
In order to work with the pickle module, we have first import it in your program using the import statement:
import pickle
And then, we may use dump() and load() methods of pickle module to write and read from an open binary file respectively.
The process of working with binary files is similar to as we have done so far with a little difference that you work with pickle module in binary files,
(i) Import pickle module.
(ii) Open binary file in the required file mode (read mode or write mode)
(iii) Process binary file by writing/reading objects using pickle module’s methods
(iv) Once done, close the file.
PICKLING & UNPICKLING
“Pickling” is the process of how Python object authority is converted into a byte-stream,
and “unpickling” is the opposite operation, whereby a byte stream is converted back into an object hierarchy.
Creating,Opening,Closing Binary Files
A binary file is opened in the same way as we open any other file but we have to make sure to use “b” with file modes to open a file in binary mode
There are two similar functions known as dump() and loads() of pickle module but,
These serialize/de-serialize objects in string form while load() and dump() serialize objects for an open binary file.
Like text files, a binary file will get created when opened in an output file mode and it does not exist already.
That is, for the file modes, “w, ” +”, “d”, the file will get created if it does not exist already but In case the file exists already, then the file modes “u” and “w+” will overwrite the file and the file mode ” ” will retain the contents of the file.
Points to Remember:
In case we are opening a binary file in the read mode, then the file needs to exist otherwise an exception is a run time error will be raised.
Also, in an existing file, when the last record is reached and the end of file(EOF) is reached,
if not handled properly. then it might return an EOT Error exception.
Thus it is important to handle exceptions while opening a file for reading.
For this Situation, it is advised to open a file in read mode either to try and accept blocks or using the statement.