Serialize and Deserialize an Open File Object in Python
Last Updated :
23 Jul, 2025
Serialization refers to the process of converting an object into a format that can be easily stored or transmitted, such as a byte stream. Deserialization, on the other hand, involves reconstructing the object from its serialized form. When dealing with file operations, it's common to serialize data before writing it to a file and deserialize it when reading it back. In this article, we'll explore how to serialize and deserialize an open file object in Python.
Serialize and Deserialize an Open File Object in Python
Below are the ways to Serialize and Deserialize an Open File Object in Python:
- Using Pickle
- Using JSON
- Using YAML
Serialize and Deserialize an Open File Object Using Pickle
This Python code serializes a dictionary `data` into a binary file named 'data.pickle' using the Pickle module, then deserializes it back into `loaded_data` from the same file, printing the result.
Python3
import pickle
# Serialize
data = {'name': 'John', 'age': 30}
with open('data.pickle', 'wb') as file:
pickle.dump(data, file)
# Deserialize
with open('data.pickle', 'rb') as file:
serialized_data = file.read()
file.seek(0)
loaded_data = pickle.load(file)
print("Type of serialized data:", type(serialized_data))
print("\nDeserialized data:", loaded_data)
print("Type of deserialized data:", type(loaded_data))
OutputType of serialized data: <class 'bytes'>
Deserialized data: {'name': 'John', 'age': 30}
Type of deserialized data: <class 'dict'>
Serialize and Deserialize an Open File Object Using JSON
This Python script serializes a dictionary `data` into a JSON file named 'data.json', then deserializes it back into `loaded_data` from the same file, printing the result.
Python3
import json
# Serialize
data = {'name': 'John', 'age': 30}
with open('data.json', 'w') as file:
json.dump(data, file)
# Deserialize
with open('data.json', 'r') as file:
serialized_data = file.read()
file.seek(0)
loaded_data = json.load(file)
print("Type of serialized data:", type(serialized_data))
print("\nDeserialized data:", loaded_data)
print("Type of deserialized data:", type(loaded_data))
OutputType of serialized data: <class 'str'>
Deserialized data: {'name': 'John', 'age': 30}
Type of deserialized data: <class 'dict'>
Serialize and Deserialize an Open File Object Using YAML
This Python script serializes a dictionary `data` into a YAML file named 'data.yaml', then deserializes it back into `loaded_data` from the same file, printing the result.
Python3
import yaml
# Serialize
data = {'name': 'John', 'age': 30}
with open('data.yaml', 'w') as file:
yaml.dump(data, file)
# Deserialize
with open('data.yaml', 'r') as file:
serialized_data = file.read()
file.seek(0)
loaded_data = yaml.safe_load(file)
print("Type of serialized data:", type(serialized_data))
print("Deserialized data:", loaded_data)
print("Type of deserialized data:", type(loaded_data))
Output
Type of serialized data: <class 'str'>
Deserialized data: {'age': 30, 'name': 'John'}
Type of deserialized data: <class 'dict'>
Similar Reads
Check a File is Opened or Closed in Python In computer programming, working with files is something we often do. Python, a programming language, gives us useful tools to handle files. One important thing to know when dealing with files is whether a file is currently open or closed. This is crucial to avoid problems and make sure the data sta
4 min read
Close a File in Python In Python, a file object (often denoted as fp) is a representation of an open file. When working with files, it is essential to close the file properly to release system resources and ensure data integrity. Closing a file is crucial to avoid potential issues like data corruption and resource leaks.
2 min read
File System Manipulation in Python File system manipulation in Python refers to the ability to perform various operations on files, such as creating, reading, writing, appending, renaming, and deleting. Python provides several built-in modules and functions that allow you to perform various file system operations. Python treats files
3 min read
Optimized I/O Operations in Python Python is widely used for file input/output (I/O) operations due to its simplicity and versatility. However, when working with large files or numerous Input/Output operations, optimizing file I/O becomes vital for efficient performance. In this article, we'll understand how to Optimize I/O operation
2 min read
Serialize and Deserialize complex JSON in Python JSON stands for JavaScript Object Notation. It is a format that encodes the data in string format. JSON is language-independent and because of that, it is used for storing or transferring data in files. Serialization of JSON object: It means converting a Python object (typically a dictionary) into a
3 min read
Serialize and Deserialize complex JSON in Python JSON stands for JavaScript Object Notation. It is a format that encodes the data in string format. JSON is language-independent and because of that, it is used for storing or transferring data in files. Serialization of JSON object: It means converting a Python object (typically a dictionary) into a
3 min read