Problem Statement
What is the main benefit of using 'with open(...) as f'?
Explanation
The with statement uses a context manager. It ensures setup and teardown happen reliably. The file handle is closed even when exceptions are raised.
This reduces resource leaks and keeps code short compared to try and finally patterns.
Code Solution
SolutionRead Only
with open('data.txt', 'r', encoding='utf-8') as f:
for line in f:
process(line)