Best use of "open" context manager

Albert-Jan Roskam sjeik_appie at hotmail.com
Fri Jul 12 05:30:14 EDT 2024


   Or like below, although pylint complains about this: "consider using
   with". Less indentation this way.
   f = None
   try:
       f = open(FILENAME)
       records = f.readlines()
   except Exception:
       sys.exit(1)
   finally:
       if f is not None:
           f.close()


More information about the Python-list mailing list