Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #22271

Re: question about file handling with "with"

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'subject:question': 0.08; 'finally:': 0.09; 'from:addr:web.de': 0.09; 'subject:file': 0.09; 'url:peps': 0.09; 'python': 0.11; 'f.close()': 0.16; 'order?': 0.16; 'received:80.91': 0.16; 'received:80.91.229': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:gmane.org': 0.16; 'received:list': 0.16; 'received:t-dialin.net': 0.16; 'returns,': 0.16; 'subject:handling': 0.16; 'def': 0.20; 'wrote:': 0.21; 'function': 0.22; 'url:dev': 0.22; 'header:User-Agent:1': 0.23; 'input': 0.24; 'equivalent': 0.27; 'subject:with': 0.28; 'file': 0.29; "i'd": 0.29; "doesn't": 0.30; 'url:python': 0.34; 'header:X -Complaints-To:1': 0.36; 'url:org': 0.36; 'skip:" 10': 0.38; 'received:org': 0.38; 'to:addr:python-list': 0.39; 'think': 0.40; 'to:addr:python.org': 0.40; 'closed': 0.61; 'skip:j 10': 0.61; 'details.': 0.61; 'yes,': 0.63; 'subject:"': 0.72; 'roughly': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: question about file handling with "with"
Date Wed, 28 Mar 2012 11:59:20 +0200
Organization None
References <CAOuJsMm2CXh9RCSk_4-PyeaBR+N9gqkbP2=gSxXxHoEsD9mTZg@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084a352.dip.t-dialin.net
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1068.1332928726.3037.python-list@python.org> (permalink)
Lines 34
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1332928726 news.xs4all.nl 6872 [2001:888:2000:d::a6]:56774
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:22271

Show key headers only | View raw


Jabba Laci wrote:

> Is the following function correct? 

Yes, though I'd use json.load(f) instead of json.loads().

> Is the input file closed in order?
> 
> def read_data_file(self):
>     with open(self.data_file) as f:
>         return json.loads(f.read())


The file will be closed when the with-block is left. That is before 
read_data_file() returns, even in a Python implementation that doesn't use 
ref-counting. Think of

with open(...) as f:
   # whatever

as roughly equivalent to

f = open(...)
try:
    # whatever
finally:
    f.close()

See the "specification" section of

http://www.python.org/dev/peps/pep-0343/

for the gory details.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: question about file handling with "with" Peter Otten <__peter__@web.de> - 2012-03-28 11:59 +0200

csiph-web