Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

O, what a tangled web we weave, When first we practice to deceive. -- Sir Walter Scott, "Marmion"


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

SubjectAuthor
o Re: Best use of "open" context managerRob Cliffe

1
Subject: Re: Best use of "open" context manager
From: Rob Cliffe
Newsgroups: comp.lang.python
Date: Sun, 7 Jul 2024 21:22 UTC
References: 1 2 3
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: Re: Best use of "open" context manager
Date: Sun, 7 Jul 2024 22:22:49 +0100
Lines: 38
Message-ID: <mailman.13.1720391145.2981.python-list@python.org>
References: <954c4ca8-cf37-4482-a1be-46d39cb503f9@btinternet.com>
<ZonqkTYNCiNQRQSv@cskk.homeip.net>
<1ed1c4b0-e89c-4dbc-8b16-35aeebce8ee3@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 fpU5X703A6frJspjFtm2DAljVn4QcqYf+5upAHhr6Omg==
Cancel-Lock: sha1:S+c/omcmH++FdRbMDq1QENzeH08= sha256:VMQa0bvDrMXUJitpbXLX46y4FEs+293MmhGd9Qwjjf8=
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=mXED46fx;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.082
X-Spam-Evidence: '*H*': 0.84; '*S*': 0.00; 'this:': 0.03; '"as': 0.09;
'valueerror:': 0.09; 'cameron': 0.16; 'remember,': 0.16;
'simpson': 0.16; 'syntax.': 0.16; 'wrote:': 0.16; 'python': 0.16;
'to:addr:python-list': 0.20; 'closed': 0.22; 'returns': 0.22;
'code': 0.23; 'lines': 0.23; 'object': 0.26; 'this?': 0.29;
'header:User-Agent:1': 0.30; "doesn't": 0.32; 'context': 0.32;
'to:name:python': 0.32; 'received:192.168.1': 0.32; 'but': 0.32;
"i'm": 0.33; 'same': 0.34; 'header:In-Reply-To:1': 0.34; 'using':
0.37; "it's": 0.37; 'received:192.168': 0.37; 'file': 0.38; 'put':
0.38; 'received:213': 0.40; 'wishes': 0.40; 'best': 0.61; 'skip:o
10': 0.61; 'skip:\xc2 10': 0.62; 'here': 0.62; '8bit%:100': 0.76;
'forgot': 0.84; 'itself.': 0.84; 'rob': 0.84; 'subject:manager':
0.84; 'subject:open': 0.84; 'legal,': 0.93
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=btinternet.com;
s=btmx201904; t=1720387370;
bh=eTMu4CzPKAfNe6b9CP9FBVJkQBihvR7Zs7/NFicEMKY=;
h=Message-ID:Date:MIME-Version:Subject:To:References:From:In-Reply-To;
b=mXED46fxpDr2jcNaF2mwoUuvP0WD2EJeGikjiR6jwu6S/NQvCbmN3/2jC1DPkfmCoKz0szvvVCKuM65N04FmKlIbbbQWIYoBuC7lNegVYmwsXMN/rL3mjBbCDQhDbLBtmrl0EQJcYxwnnTIjdv1gf3vfVbKpqkUjvEQFhbdnUB3J+QqBlD/2hWs+H//5aWl+cSdGTcYR33rtounRAvmQOPFe0NfBxH8w9T4sDgI2KD1msOI2+0wWNl6Q4cVaqiySOP/AFmH02ucdGwlXrGVcFtu8fCLQcmahCdGF/ZNfFEmXF8Oe/4kOhG88hX9itYY4euNvpVrphEBwTyK6OZ1p7w==
Authentication-Results: btinternet.com;
auth=pass (PLAIN) smtp.auth=rob.cliffe@btinternet.com;
bimi=skipped
X-SNCR-Rigid: 656684A0187AA8F5
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: gggruggvucftvghtrhhoucdtuddrgeeftddrvdehgdduheejucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuueftkffvkffujffvgffngfevqffopdfqfgfvnecuuegrihhlohhuthemuceftddunecunecujfgurhepkfffgggfuffvfhfhjggtgfesthekredttddvjeenucfhrhhomheptfhosgcuvehlihhffhgvuceorhhosgdrtghlihhffhgvsegsthhinhhtvghrnhgvthdrtghomheqnecuggftrfgrthhtvghrnhepteetueffteffgeetffehveehjeduffekleetudevkeetkeehteevudejgeekheevnecukfhppeekiedrudefledrudeljedrjeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhephhgvlhhopegludelvddrudeikedruddrvddtkegnpdhinhgvthepkeeirddufeelrdduleejrdejpdhmrghilhhfrhhomheprhhosgdrtghlihhffhgvsegsthhinhhtvghrnhgvthdrtghomhdpnhgspghrtghpthhtohepuddprhgtphhtthhopehphihthhhonhdqlhhishhtsehphihthhhonhdrohhrghdprhgvvhfkrfephhhoshhtkeeiqddufeelqdduleejqdejrdhrrghnghgvkeeiqddufeelrdgsthgtvghnthhrrghlphhluhhsrdgtohhmpdgruhhthhgpuhhsvghrpehrohgsrdgtlhhifhhfvgessghtihhnthgvrhhnvghtrdgtohhmpdhgvghokffrpefiuedpoffvtefjohhsthepshgrqdhprhguqdhrghhouhhtqddttddu
X-RazorGate-Vade-Verdict: clean 0
X-RazorGate-Vade-Classification: clean
X-SNCR-hdrdom: btinternet.com
User-Agent: Mozilla Thunderbird
Content-Language: en-GB
In-Reply-To: <ZonqkTYNCiNQRQSv@cskk.homeip.net>
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: <1ed1c4b0-e89c-4dbc-8b16-35aeebce8ee3@btinternet.com>
X-Mailman-Original-References: <954c4ca8-cf37-4482-a1be-46d39cb503f9@btinternet.com>
<ZonqkTYNCiNQRQSv@cskk.homeip.net>
View all headers

On 07/07/2024 02:08, Cameron Simpson wrote:
> On 06Jul2024 11:49, Rob Cliffe <rob.cliffe@btinternet.com> wrote:
>> 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 .....
>
> What about this:
>
>     try:
>         f = open(FileName) as f:
>     except FileNotFoundError:
>         print(f"File {FileName} not found")
>         sys.exit()
>     with f:
>         ... process the lines here ...
>
> Remember, the `open()` call returns a file object _which can be used
> as a context manager_. It is separate from the `with` itself.
Did you test this?
    f = open(FileName) as f:
is not legal syntax.
If you omit the "as f:"
it's legal, but doesn't work (trying to access the file after "with f"
raises the same
    ValueError: I/O operation on closed file.
I'm using Python 3.11.5.

Best wishes
Rob Cliffe

1

rocksolid light 0.9.8
clearnet tor