English

Write the use and syntax for the following method: dump()

Advertisements
Advertisements

Question

Write the use and syntax for the following method:

dump()

Short/Brief Note
Advertisements

Solution

dump() method is used to convert (pickling) Python objects for writing data in a binary file. The file in which data are to be dumped, needs to be opened in binary write mode (wb).

Syntax of dump() is as follows:

dump(data_object, file_object)

where data_object is the object that has to be dumped to the file with the file handle named file_object.

1 import pickle
2 listavalues = [1, "Anjeev Singh", 'M', 36]
3 fileobject = open("mybinary.dat", "wb")
4 pickle.dump(listvalues, fileobject)
5 fileobject.close()
shaalaa.com
The Pickle Module in Python
  Is there an error in this question or solution?
Chapter 2: File Handling in Python - Exercise [Page 37]

APPEARS IN

NCERT Computer Science [English] Class 12
Chapter 2 File Handling in Python
Exercise | Q 2. d) | Page 37
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×