KnowledgeBoat Logo

Computer Science

Which of the following statements are true regarding the opening modes of a file ?

  1. When you open a file for reading, if the file does not exist, an error occurs.
  2. When you open a file for writing, if the file does not exist, an error occurs.
  3. When you open a file for reading, if the file does not exist, the program will open an empty file.
  4. When you open a file for writing, if the file does not exist, a new file is created.
  5. When you open a file for writing, if the file exists, the existing file is overwritten with the new file.

Python File Handling

1 Like

Answer

When you open a file for reading, if the file does not exist, an error occurs.
When you open a file for writing, if the file does not exist, a new file is created.
When you open a file for writing, if the file exists, the existing file is overwritten with the new file.

Reason —

  1. When you open a file for writing, if the file does not exist, an error occurs — False.
    When we open a file for writing ("w" mode) and the file does not exist, Python creates a new file.
  2. When you open a file for reading, if the file does not exist, the program will open an empty file — False.
    When we try to open a file for reading ("r" mode) that does not exist, Python raises a FileNotFoundError. It does not create an empty file.

Answered By

2 Likes


Related Questions