What is the main purpose of the with statement when working with files in Python?

Options

  • A. It automatically closes the file after the block completes
  • B. It permanently deletes the file
  • C. It converts a text file into binary format
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

A. It automatically closes the file after the block completes

Detailed Explanation

The with statement uses a context manager. File resources are automatically released when the block exits.