1. Home
  2. Data Science
  3. Python
  4. Why is the with statement recommended when working with…

Added 25 Nov 2026

Why is the with statement recommended when working with files in Python?

  1. It makes the file faster
  2. It encrypts data
  3. It closes the file automatically Correct answer
  4. It locks the file permanently

Correct answer

C. It closes the file automatically

MCQSEXAM

Explanation

The context manager ensures the file is closed even if an error occurs.

Reference

Asked in: Python Interview File Handling

https://docs.python.org/3/tutorial/inputoutput.html

Preparing for an exam?

Doubts & discussion

Not sure why the answer is C? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More Python MCQs

Practise all Python MCQs

Similar MCQs