1. What is the main benefit of using 'with open(...) as f'?
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.
with open('data.txt', 'r', encoding='utf-8') as f:
for line in f:
process(line)