Computer Science
Identify the error in the following code.
import pickle
data = ['one', 2, [3, 4, 5]]
with open('data.dat', 'wb' :
pickle.dump(data)
Python File Handling
3 Likes
Answer
import pickle
data = ['one', 2, [3, 4, 5]]
with open('data.dat', 'wb' : #error 1
pickle.dump(data) #error 2
- There is a syntax error in the
open()
function call. The closing parenthesis is missing in theopen()
function call. Also, file handle is not mentioned. - The
pickle.dump()
function requires two arguments - the object to be pickled and the file object to which the pickled data will be written. However, in the provided code, thepickle.dump()
function is missing the file object argument.
The corrected code is :
import pickle
data = ['one', 2, [3, 4, 5]]
with open('data.dat', 'wb') as f:
pickle.dump(data, f)
Answered By
1 Like
Related Questions
Identify the error in the following code.
import csv f = open('attendees1.csv') csv_f = csv.writer(f)
Identify the error in the following code.
import csv f = open('attendees1.csv') csv_f = csv.reader() for row in csv_f: print(row)
Write a program that reads a text file and creates another file that is identical except that every sequence of consecutive blank spaces is replaced by a single space.
A file sports.dat contains information in following format:
Event - Participant
Write a function that would read contents from file sports.dat and creates a file named Atheletic.dat copying only those records from sports.dat where the event name is "Atheletics".