Advertisements
Advertisements
Question
Write a function that writes a structure to disk and then reads it back and display on screen.
Code Writing
Advertisements
Solution
A Python structure such as a dictionary can be stored and retrieved using the pickle module.
import pickle
def write_read_structure():
record = {
"code": 101,
"name": "Notebook",
"price": 50,
"quantity": 10
}
with open("item.dat", "wb") as file:
pickle.dump(record, file)
with open("item.dat", "rb") as file:
retrieved_record = pickle.load(file)
print(retrieved_record)
write_read_structure()shaalaa.com
Is there an error in this question or solution?
