CBSE Class 12 Computer Science
Question 39 of 136
Data File Handling — Question 4
Back to all questions 4
Question Which of the following statements is/are true?
- When we open a file for reading, if the file does not exist, an error occurs.
- When we open a file for writing, if the file does not exist, a new file is created.
- When we open a file for writing, if the file exists, the existing file is overwritten with the new file.
- All of these.
All of these.
Reason — When we open a file for reading ('r' mode) that doesn't exist, Python raises an error (FileNotFoundError). When we open a file for writing ('w' mode) and the file doesn't exist, a new file is created with the specified name. If we open a file for writing ('w' mode) and the file already exists, the existing file is overwritten with the new file.