Python files and context managers
A context manager gives a resource a well-defined lifetime. A with block closes a file even if an exception occurs while using it.
Python
from pathlib import Path
from tempfile import TemporaryDirectory
with TemporaryDirectory() as folder:
path = Path(folder) / "notes.txt"
path.write_text("Learn\nPractice\n", encoding="utf-8")
with path.open(encoding="utf-8") as handle:
lines = [line.strip() for line in handle]
print(len(lines))
print(lines[1])