Sujet : Re: Best use of "open" context manager
De : ram (at) *nospam* zedat.fu-berlin.de (Stefan Ram)
Groupes : comp.lang.pythonDate : 12. Jul 2024, 13:55:13
Autres entêtes
Organisation : Stefan Ram
Message-ID : <code-20240712125444@ram.dialup.fu-berlin.de>
References : 1 2 3
Albert-Jan Roskam <
sjeik_appie@hotmail.com> schrieb oder zitierte:
Or like below, although pylint complains about this: "consider using
with". Less indentation this way.
f = None
try:
f = open(FILENAME)
records = f.readlines()
This try clause would also catch exception raised by the readlines
call, and I thought that this was what the OP was looking to avoid.
except Exception:
sys.exit(1)
When this code is part of a library, it might not be appropriate
to make the whole application exit at this point.