Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

BOFH excuse #245: The Borg tried to assimilate your system. Resistance is futile.


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

SubjectAuthor
o Re: Best use of "open" context managerCameron Simpson

1
Subject: Re: Best use of "open" context manager
From: Cameron Simpson
Newsgroups: comp.lang.python
Date: Sun, 7 Jul 2024 23:02 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: cs@cskk.id.au (Cameron Simpson)
Newsgroups: comp.lang.python
Subject: Re: Best use of "open" context manager
Date: Mon, 8 Jul 2024 09:02:38 +1000
Lines: 37
Message-ID: <mailman.18.1720393364.2981.python-list@python.org>
References: <1ed1c4b0-e89c-4dbc-8b16-35aeebce8ee3@btinternet.com>
<Zoseju8zFLxM-s4r@cskk.homeip.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de hdR/txUCqlJCEqlyuYt4ogYa+kr81myHQQYXMWrBCJTg==
Cancel-Lock: sha1:f2lIhDvUCMdxs/v8a4Y3VhgtU7U= sha256:iu2z47/1Flwt0p8cJO7pSqndjcqAyWdfTnQSnVXnJQY=
Return-Path: <cameron@cskk.id.au>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=none reason="no signature";
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.006
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '"as': 0.09; 'cc:addr
:python-list': 0.09; 'line:': 0.09; 'valueerror:': 0.09; '22:22,':
0.16; 'cc:name:python': 0.16; 'filename': 0.16; 'from:addr:cs':
0.16; 'from:addr:cskk.id.au': 0.16; 'from:name:cameron simpson':
0.16; 'message-id:@cskk.homeip.net': 0.16; 'python3': 0.16;
'received:13.237': 0.16; 'received:13.237.201': 0.16;
'received:13.237.201.189': 0.16; 'received:cskk.id.au': 0.16;
'received:id.au': 0.16; 'received:l': 0.16;
'received:mail.cskk.id.au': 0.16; 'syntax.': 0.16; 'wrote:': 0.16;
'cc:addr:python.org': 0.20; "i've": 0.22; 'closed': 0.22;
'returns': 0.22; 'code': 0.23; 'lines': 0.23; 'run': 0.23;
'cc:2**0': 0.25; 'object': 0.26; 'this?': 0.29; 'header:User-
Agent:1': 0.30; "doesn't": 0.32; 'context': 0.32; 'right,': 0.32;
'but': 0.32; 'script': 0.33; 'same': 0.34; 'header:In-Reply-To:1':
0.34; 'received:au': 0.35; 'file': 0.38; 'text': 0.39; 'file:':
0.40; 'skip:o 10': 0.61; 'me.': 0.62; 'here': 0.62; 'received:13':
0.64; 'received:userid': 0.66; 'itself.': 0.84; 'rob': 0.84;
'subject:manager': 0.84; 'subject:open': 0.84; 'demo': 0.91;
'legal,': 0.93
Mail-Followup-To: Rob Cliffe <rob.cliffe@btinternet.com>,
Python <python-list@python.org>
Content-Disposition: inline
In-Reply-To: <1ed1c4b0-e89c-4dbc-8b16-35aeebce8ee3@btinternet.com>
User-Agent: Mutt/2.2.13 (2024-03-09)
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: <Zoseju8zFLxM-s4r@cskk.homeip.net>
X-Mailman-Original-References: <1ed1c4b0-e89c-4dbc-8b16-35aeebce8ee3@btinternet.com>
View all headers

On 07Jul2024 22:22, Rob Cliffe <rob.cliffe@btinternet.com> wrote:
>>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.

No. You're right, remove 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.

This astounds me. Code snippet to demo this?

Here's a test script which I've just run now:

FileName = 'foo.txt'
try:
f = open(FileName)
except FileNotFoundError:
print(f"File {FileName} not found")
sys.exit()
with f:
for line in f:
print("line:", line.rstrip())

Here's the foo.txt file:

here are
some lines of text

Here's the run:

% python3 p.py
line: here are
line: some lines of text

1

rocksolid light 0.9.8
clearnet tor