Close a file on every return path
The function returns after opening a file, so the handle is never explicitly closed.
def first_line(path):
handle = open(path, encoding="utf-8")
return handle.readline()
Use a context manager so the handle is closed when the function leaves the block.
Which change reliably closes the file after reading?
Why it matters: Using a context manager guarantees cleanup on both successful and exceptional paths.