← Quiz: Context Managers: all 10 questions
Quiz: Context Managers · Question 9 of 10
Why is try/finally alone used less often than with for files?
Why is try/finally alone used less often than with for files?
- A.try/finally cannot close files
- B.try/finally is slower
- C.with does the same job in fewer lines and is harder to get wrong
- D.with works only in Python 2
Answer & explanation
Correct answer: with does the same job in fewer lines and is harder to get wrong
Both are correct, but with puts the setup and clean-up in one reusable place.
Can you get the rest right without peeking?
Take the timed Quiz: Context Managers quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →