CBSE Class 12 Computer Science Question 118 of 145

File Handling — Question 14

Back to all questions
14
Question

Question 14(a)

Identify the error in the following code.

import csv
f = open('attendees1.csv')
csv_f = csv.writer(f)
Answer
import csv
f = open('attendees1.csv') #error
csv_f = csv.writer(f)

To use the csv.writer() function, the file should be opened in write mode ('w'). The corrected code is :

import csv
f = open('attendees1.csv', 'w')
csv_f = csv.writer(f)