A with statement manages a context for you and performs defined cleanup when the context ends. With files, it closes the file automatically, even when an exception occurs. section 14.0
Reading a menu safely
Instead of opening a file and leaving it open, write: with open(filename) as f: result = [item.strip() for item in f.readlines()]. The indented code reads the menu, and Python closes the file afterward.





