Binary File - dump() Function


☞ To write data on a binary file, we use dump() function.

☞ import a pickle module to use this function.

☞ It is used to serialize the object like list, dictionary etc into byte stream.

Syntax : pickle.dump("object","file_object")

Example

 #writing data on a binary file. 

import pickle

def writing_data():
    f=open("demo.dat","wb")
    while True:
        name=input("Enter name : ")
        age=input("Enter age : ")
        record=[name, age]
        pickle.dump(record,f)
        choice=input("Do you wish to continue [y/n]: ")
        if choice=='n':
            break        
    f.close()

writing_data() #calling a function