The following code is used for pickling Python objects. Arrange these code fragments in correct order-
(A). pickle.dump(list, fileobj)
(B). fileobj= open("myfile.dat", "wb")
(C). list= [1, "John", 'f', 25]
(D). fileobj.close( )
The following code is used for pickling Python objects. Arrange these code fragments in correct order-
(A). pickle.dump(list, fileobj)
(B). fileobj= open("myfile.dat", "wb")
(C). list= [1, "John", 'f', 25]
(D). fileobj.close( )
Solution
The object to be pickled must exist first, then the file must be opened in binary write mode, then the object is dumped into it, and finally the file is closed.
(C) list= [1, "John", 'f', 25] creates the object
(B) fileobj= open("myfile.dat", "wb") opens the file for binary writing
(A) pickle.dump(list, fileobj) writes the object to the file
(D) fileobj.close( ) closes the file
Option 2: closes the file before dumping, so the write would fail on a closed file.
Pickling needs the file opened in "wb" (binary) mode, because pickle writes bytes, not text.
Related questions:
2026: 29 May Shift 2
2025: 20 May Shift 1
2025: 3 June Shift 2
2025: 15 May Shift 2
2026: 11 May Shift 2