Computer Science

Which of the following statements is/are true?

  1. When we open a file for reading, if the file does not exist, an error occurs.
  2. When we open a file for writing, if the file does not exist, a new file is created.
  3. When we open a file for writing, if the file exists, the existing file is overwritten with the new file.
  4. All of these.

Python Data Handling

1 Like

Answer

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.

Answered By

2 Likes


Related Questions