Context Manager in Python
A context manager sets up and cleans up automatically. with expression as var: guarantees cleanup on the way out — even if an error fires. Files: with open(path) as f:. You can write your own with __enter__ / __exit__ or contextlib. Mental model: acquire, use, release. with is safer than open + close that you might forget.
Why with beats manual close() — an exception in the middle still closes the file. That sentence is the viva.
Context Manager in Python — output — Hi. with guarantees close — same idea as with open("a.txt").
Why with is safer than manual close().