What happens when a file is opened using the w mode in Python?

Options

  • A. The file is opened only for reading
  • B. The file is opened for writing and an existing file is truncated
  • C. The file is opened only for appending
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

B. The file is opened for writing and an existing file is truncated

Detailed Explanation

The w mode opens a file for writing. It creates a new file if necessary and truncates an existing file to zero length.