Path: ...!fu-berlin.de!uni-berlin.de!not-for-mail From: ram@zedat.fu-berlin.de (Stefan Ram) Newsgroups: comp.lang.python Subject: Re: Best use of "open" context manager Date: 12 Jul 2024 11:55:13 GMT Organization: Stefan Ram Lines: 17 Expires: 1 Feb 2025 11:59:58 GMT Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de 4yPAk+/uO/EvWB3cwib2XwDkv1fd44UJY8H3rwTr53+d8A Cancel-Lock: sha1:E5E+6zarKdjcQ1uWVhWgJsGTxV0= sha256:B/QTMtW+rT2etISOxUbTh9zCZGFh5T9wVR8xdQiPoxo= X-Copyright: (C) Copyright 2024 Stefan Ram. All rights reserved. Distribution through any means other than regular usenet channels is forbidden. It is forbidden to publish this article in the Web, to change URIs of this article into links, and to transfer the body without this notice, but quotations of parts in other Usenet posts are allowed. X-No-Archive: Yes Archive: no X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some services to mirror the article in the web. But the article may be kept on a Usenet archive server with only NNTP access. X-No-Html: yes Content-Language: de-DE-1901, en-US Bytes: 2038 Albert-Jan Roskam 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.