Computer Science

Differentiate between the following :

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

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

Python File Handling

2 Likes

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.

Answered By

1 Like


Related Questions