CBSE Class 12 Computer Science Question 104 of 145

File Handling — Question 21

Back to all questions
21
Question

Question 21

Differentiate between the following :

(i) f = open('diary.txt', 'r')

(ii) f = open('diary.txt', 'w')

Answer

(i) f = open('diary.txt', 'r') — This line opens the file diary.txt in read mode ('r'). If the file does not exist, Python will raise an error. If the file exists, the data will not be erased.

(ii) f = open('diary.txt', 'w') — This line opens the file diary.txt in write mode ('w'). If the file does not exist, Python creates new file with the specified name. If the file exists, Python will truncate existing data and over-write in the file.