Loading notes...
Loading notes...
B.Tech • Chapter 6
B.Tech level module on File I/O and Serialization.
Interacting with the filesystem is fundamental. B.Tech engineers must understand buffers, file modes, encoding (UTF-8), and serialization formats like JSON and Pickle.
Advanced Engineering Concepts
Using the 'with' statement (Context Managers) ensures that file resources are properly cleaned up and released back to the OS, even if an exception occurs during execution. Serialization (Pickling) converts Python object hierarchies into byte streams for storage or network transmission. However, Pickle is Python-specific and insecure for untrusted data, making JSON the preferred standard for web APIs.
Code Implementation
import json
import pickle
data = {"course": "B.Tech", "subjects": ["DSA", "OS", "DBMS"]}
# JSON Serialization (Cross-language, text-based)
with open("data.json", "w") as f:
json.dump(data, f)
# Pickle Serialization (Python-specific, binary)
with open("data.pkl", "wb") as f:
pickle.dump(data, f)
# Context manager guarantees file.close() is called