Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

BOFH excuse #211: Lightning strikes.


comp / comp.lang.python / Best use of "open" context manager

SubjectAuthor
* Best use of "open" context managerRob Cliffe
`* Re: Best use of "open" context managerStefan Ram
 `- Re: Best use of "open" context manager (Posting On Python-List Prohibited)Lawrence D'Oliveiro

1
Subject: Best use of "open" context manager
From: Rob Cliffe
Newsgroups: comp.lang.python
Date: Sat, 6 Jul 2024 10:49 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: rob.cliffe@btinternet.com (Rob Cliffe)
Newsgroups: comp.lang.python
Subject: Best use of "open" context manager
Date: Sat, 6 Jul 2024 11:49:06 +0100
Lines: 61
Message-ID: <mailman.3.1720263159.2981.python-list@python.org>
References: <954c4ca8-cf37-4482-a1be-46d39cb503f9@btinternet.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de gQdxhI1pAIs1TQsn0wRkHw3QqkDK0T8jycAkF8Sk624g==
Cancel-Lock: sha1:GtwKgiJ4ECETiawHu8r3uhbW9Ao= sha256:eUG9M80DVoU6KgPqoJ6Zt6sGpakngDxxyycQ8Cs98h8=
Return-Path: <rob.cliffe@btinternet.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=btinternet.com header.i=@btinternet.com header.b=Yd06McLN;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.079
X-Spam-Evidence: '*H*': 0.85; '*S*': 0.00; 'this:': 0.03; '(which':
0.04; 'e.g.': 0.07; 'action,': 0.09; 'else:': 0.09; 'memory.':
0.09; 'valueerror:': 0.09; 'decreases': 0.16; 'pythonic': 0.16;
'targeted': 0.16; 'to:addr:python-list': 0.20; 'closed': 0.22;
'ran': 0.22; 'code': 0.23; 'lines': 0.23; 'seems': 0.26;
'manager,': 0.26; 'error': 0.29; 'header:User-Agent:1': 0.30;
'attempt': 0.31; 'modify': 0.31; 'context': 0.32;
'to:name:python': 0.32; 'received:192.168.1': 0.32; 'but': 0.32;
'there': 0.33; 'work,': 0.36; 'really': 0.37; 'using': 0.37;
'received:192.168': 0.37; 'file': 0.38; 'could': 0.38; 'put':
0.38; 'read': 0.38; 'text': 0.39; 'program.': 0.40; 'wishes':
0.40; 'something': 0.40; 'want': 0.40; 'should': 0.40; 'best':
0.61; 'kept': 0.61; 'skip:o 10': 0.61; 'here': 0.62; 'small,':
0.69; 'suite': 0.71; 'exceptions': 0.84; 'forgot': 0.84;
'readability.': 0.84; 'rob': 0.84; 'subject:manager': 0.84;
'subject:open': 0.84; 'violates': 0.84; 'loses': 0.91; 'race':
0.93
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=btinternet.com;
s=btmx2012311; t=1720263158;
bh=+mGosCyMsnajjC1Ha7qejdTZIpqrKg/+1TunNUBJRTI=;
h=Message-ID:Date:MIME-Version:To:From:Subject;
b=Yd06McLN9bzfKpebjxhSyIIzYLoWo64IMvPAaw7pLfgZ7iW1NPfHqyH1cQY0qIziad+xyTgJSKF1iitZcDjoY5s3uC0153AnuPzm8DHAB0bsMUsHiINObQRM3z3nRtgfhzx82J0GwEzQxIx+e4XsYY8kUvMDxCiJmUuvvI4Ee4WliZVuX1E6vtXExSZuSUFZaye2J0B0TVM47RxTIzP5Lf8NFtFcdR2FpUm0GgExaVassstQULBkOu0PUHZ9OO2xpb7WuIIreKPTKW1ikb1OvJ/2jx94Zux7daEEqbjDnwLHw0nUmAUYzq7KNin45J29DC/odr3Xl6LC58HoFcvMSg==
X-SNCR-Rigid: 666A0E5601835497
X-Originating-IP: [86.139.197.7]
X-OWM-Source-IP: 86.139.197.7
X-OWM-Env-Sender: rob.cliffe@btinternet.com
X-VadeSecure-score: verdict=clean score=0/300, class=clean
X-RazorGate-Vade: gggruggvucftvghtrhhoucdtuddrgeeftddrvdefgdeffecutefuodetggdotefrodftvfcurfhrohhfihhlvgemuceutffkvffkuffjvffgnffgvefqofdpqfgfvfenuceurghilhhouhhtmecufedtudenucenucfjughrpefkffggfgfvhffutgfgsehtkeertddtvdejnecuhfhrohhmpeftohgsucevlhhifhhfvgcuoehrohgsrdgtlhhifhhfvgessghtihhnthgvrhhnvghtrdgtohhmqeenucggtffrrghtthgvrhhnpeejieegffeivefhjeduhefhjeetgfeuffdvgfeggfdvteeftedufeevuefghefhteenucfkphepkeeirddufeelrdduleejrdejnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehhvghloheplgduledvrdduieekrddurddvtdekngdpihhnvghtpeekiedrudefledrudeljedrjedpmhgrihhlfhhrohhmpehrohgsrdgtlhhifhhfvgessghtihhnthgvrhhnvghtrdgtohhmpdhnsggprhgtphhtthhopedupdhrtghpthhtohepphihthhhohhnqdhlihhsthesphihthhhohhnrdhorhhgpdhrvghvkffrpehhohhsthekiedqudefledqudeljedqjedrrhgrnhhgvgekiedqudefledrsghttggvnhhtrhgrlhhplhhushdrtghomhdprghuthhhpghushgvrheprhhosgdrtghlihhffhgvsegsthhinhhtvghrnhgvthdrtghomhdpghgvohfkrfepifeupdfovfetjfhoshhtpegsthhprhgurhhgohdttdeg
X-RazorGate-Vade-Verdict: clean 0
X-RazorGate-Vade-Classification: clean
X-SNCR-hdrdom: btinternet.com
X-SNCR-hdrdom: btinternet.com
X-SNCR-hdrdom: btinternet.com
User-Agent: Mozilla Thunderbird
Content-Language: en-GB
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <954c4ca8-cf37-4482-a1be-46d39cb503f9@btinternet.com>
View all headers

Consider this scenario (which I ran into in real life):
    I want to open a text file and do a lot of processing on the lines
of that file.
    If the file does not exist I want to take appropriate action, e.g.
print an error message and abort the program.
I might write it like this:

try:
    with open(FileName) as f:
        for ln in f:
            print("I do a lot of processing here")
            # Many lines of code here .....
except FileNotFoundError:
    print(f"File {FileName} not found")
    sys.exit()

but this violates the principle that a "try" suite should be kept small,
so that only targeted exceptions are trapped,
not to mention that having "try" and "except" far apart decreases
readability.

Or I might write it like this:

try:
    f = open(FileName) as f:
    FileLines = f.readlines()
except FileNotFoundError:
    print(f"File {FileName} not found")
    sys.exit()
# I forgot to put "f.close()" here -:)
for ln in File Lines:
        print("I do a lot of processing here")
        # Many lines of code here .....

but this loses the benefits of using "open" as a context manager,
and would also be unacceptable if the file was too large to read into
memory.

Really I would like to write something like

try:
    with open(FileName) as f:
except FileNotFoundError:
    print(f"File {FileName} not found")
    sys.exit()
else: # or "finally:"
        for ln in f:
            print("I do a lot of processing here")
            # Many lines of code here .....

but this of course does not work because by the time we get to "for ln
in f:" the file has been closed so we get
ValueError: I/O operation on closed file

I could modify the last attempt to open the file twice, which would
work, but seems like a kludge (subject to race condition, inefficient).

Is there a better / more Pythonic solution?

Best wishes
Rob Cliffe

Subject: Re: Best use of "open" context manager
From: Stefan Ram
Newsgroups: comp.lang.python
Organization: Stefan Ram
Date: Sat, 6 Jul 2024 11:46 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!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: 6 Jul 2024 11:46:33 GMT
Organization: Stefan Ram
Lines: 25
Expires: 1 Feb 2025 11:59:58 GMT
Message-ID: <file-20240706124604@ram.dialup.fu-berlin.de>
References: <mailman.3.1720263159.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 ffjyw+QfkMR+fc7NaoYrsw1mXqm9DeSixklkIUcLqu0CYs
Cancel-Lock: sha1:lxo7BN/do7UPhOmnivltP46OnrU= sha256:13tA498KcTKhadE56e4r6TwTs4RNJmjpcmyBjP1j+qU=
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: en-US
View all headers

Rob Cliffe <rob.cliffe@btinternet.com> wrote or quoted:
>try:
>     with open(FileName) as f:
>except FileNotFoundError:
>     print(f"File {FileName} not found")
>     sys.exit()
>else: # or "finally:"
>         for ln in f:
>             print("I do a lot of processing here")
>             # Many lines of code here .....
>but this of course does not work because by the time we get to "for ln
>in f:" the file has been closed so we get
>ValueError: I/O operation on closed file

try:
f = open( FileName )
except FileNotFoundError:
print( f"File {FileName} not found" )
sys.exit()
else:
with f:
# put this into a separate function if it gets too long here.
for ln in f:
print( "I do a lot of processing here" )
            # Many lines of code here .....

Subject: Re: Best use of "open" context manager (Posting On Python-List Prohibited)
From: Lawrence D'Oliv
Newsgroups: comp.lang.python
Organization: A noiseless patient Spider
Date: Sun, 7 Jul 2024 03:49 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ldo@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.lang.python
Subject: Re: Best use of "open" context manager (Posting On Python-List
Prohibited)
Date: Sun, 7 Jul 2024 03:49:44 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 28
Message-ID: <v6d38o$6dlv$3@dont-email.me>
References: <mailman.3.1720263159.2981.python-list@python.org>
<file-20240706124604@ram.dialup.fu-berlin.de>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 07 Jul 2024 05:49:45 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="68b1498c9728a898ae95fa9c50f05ec1";
logging-data="210623"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19jQMHm4FPDQWgu08yH2fMN"
User-Agent: Pan/0.158 (Avdiivka; )
Cancel-Lock: sha1:V9b//m40ACoJVLvDFQxg/UQLlLw=
View all headers

On 6 Jul 2024 11:46:33 GMT, Stefan Ram wrote:

>>but this of course does not work because by the time we get to "for ln
>>in f:" the file has been closed so we get ValueError: I/O operation on
>>closed file
>
> try:
> f = open( FileName )
> except FileNotFoundError:
> print( f"File {FileName} not found" )
> sys.exit()
> else:
> with f:
> # put this into a separate function if it gets too long here.
> for ln in f:
> print( "I do a lot of processing here" )
>             # Many lines of code here .....

f = open(filename, "rt")
for ln in f :
... do your processing ...

1) Let the error exception be reported directly, whether it’s “file not
found”, or “permission error”, or some other reason; why bother to handle
it when you don’t even know what to do anyway?
2) Notice that a file open for reading automatically gets closed when it
goes out of scope (feature of CPython and all the other good
implementations).

1

rocksolid light 0.9.8
clearnet tor