
Understanding the Pickle Module
In Python, the pickle module functions much like vacuum-sealing food—it serialises data structures for storage and ensures they can be restored later in their original state. This is useful when saving complex objects, such as lists or dictionaries, to a file for future use.
The Pickle module enables serialisation and de-serialisation of Python objects.
- Serialisation (pickling): Converts objects like lists, dictionaries, and tuples into a byte stream that can be saved in a binary file.
- De-serialisation (unpickling): Restores the object to its original form.
To work with the Pickle module, follow these steps.
- Import the module using
import pickle
. - Write objects to a binary file using the
dump()
method. - Read objects from a binary file using the
load()
method.
Working with the Pickle Module
Writing to a Binary File
To write an object to a binary file, open the file in write mode (wb) and use the dump()
method.
Example:
import pickle
Dfile = open("stu.dat", "wb")
pickle.dump(["Alice", "Bob", "Charlie"], Dfile)
Dfile.close()
Reading from a Binary File
To read data from a binary file, open the file in read mode (rb) and use the load()
method.
Example:
import pickle
Dfile = open("stu.dat", "rb")
data = pickle.load(Dfile)
print(data)
Dfile.close()
Handling EOFError
When reading from a file, an EOFError
occurs when the end of the file is reached. This can be handled using try...except
.
Example:
try:
data = pickle.load(Dfile)
except EOFError:
Dfile.close()
Alternatively, the with statement automatically handles file closing:
with open("stu.dat", "rb") as Dfile:
data = pickle.load(Dfile)
Key Features of Pickle
- Preserves data structures – Ideal for storing structured data such as machine learning models and game progress.
- Efficient storage – Saves space by converting objects into binary format.
- Ensures data consistency – Restores objects exactly as they were before saving.
Conclusion
The Pickle module is a powerful tool for handling complex Python objects in binary files. It ensures that data remains unchanged and can be retrieved easily.