मराठी

Write a function that writes a structure to disk and then reads it back and display on screen.

Advertisements
Advertisements

प्रश्न

Write a function that writes a structure to disk and then reads it back and display on screen.

कोड लेखन
Advertisements

उत्तर

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
  या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
पाठ 7: Data File Handling - EXERCISE [पृष्ठ १४५]

APPEARS IN

सीबीएसई Computer Science with Python [English] Class 12
पाठ 7 Data File Handling
EXERCISE | Q 10. | पृष्ठ १४५
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×