| Deutsch English Français Italiano |
|
<code-20240712125444@ram.dialup.fu-berlin.de> View for Bookmarking (what is this?) Look up another Usenet article |
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: <code-20240712125444@ram.dialup.fu-berlin.de>
References: <ZotSwXK2ajuqmWHq@cskk.homeip.net> <DB9PR10MB66894E047D279633FA7E7B9883A62@DB9PR10MB6689.EURPRD10.PROD.OUTLOOK.COM> <mailman.33.1720776627.2981.python-list@python.org>
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 <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.