Deutsch   English   Français   Italiano  
<mailman.62.1730394380.4695.python-list@python.org>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!3.eu.feeder.erje.net!feeder.erje.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: MRAB <python@mrabarnett.plus.com>
Newsgroups: comp.lang.python
Subject: Re: Using 'with open(...) as ...' together with
 configparser.ConfigParser.read
Date: Thu, 31 Oct 2024 17:06:11 +0000
Lines: 46
Message-ID: <mailman.62.1730394380.4695.python-list@python.org>
References: <87plnj3te6.fsf@zedat.fu-berlin.de>
 <slrnvi2035.372.jon+usenet@raven.unequivocal.eu>
 <87bjz1vj2c.fsf@zedat.fu-berlin.de>
 <slrnvi4ksp.372.jon+usenet@raven.unequivocal.eu>
 <87r07xtwg7.fsf@zedat.fu-berlin.de>
 <slrnvi4ss3.372.jon+usenet@raven.unequivocal.eu>
 <87y124db0q.fsf@zedat.fu-berlin.de>
 <a88893a0-627d-4d30-83d1-ec91b318bf99@mrabarnett.plus.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de PzcuV38OtwBcYApMqgMU/gKp00LCcGW2275lhcvNJB+Q==
Cancel-Lock: sha1:L78kObbyPz2rya0yKD2mMrxVpXo= sha256:J6z3qrlD124zyIAklQVvL04R2Vzjftw0l/wywtcBOWw=
Return-Path: <python@mrabarnett.plus.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=plus.com header.i=@plus.com header.b=JgSz/v68;
 dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.007
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'containing': 0.05; ':-)':
 0.09; 'docs,': 0.09; 'example.': 0.09; 'from:addr:python': 0.09;
 'ok,': 0.09; 'received:192.168.1.64': 0.09; 'string,': 0.09;
 'writes:': 0.09; '>>>>': 0.16; '>>>>>': 0.16; '>>>>>>': 0.16;
 'bennett': 0.16; 'dict': 0.16; 'file-like': 0.16; 'folder.': 0.16;
 'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16;
 'functions,': 0.16; 'indeed': 0.16; 'instead.': 0.16; 'message-
 id:@mrabarnett.plus.com': 0.16; 'received:plus.net': 0.16;
 'round.': 0.16; 'stream.': 0.16; 'wrote:': 0.16; "can't": 0.17;
 'to:addr:python-list': 0.20; 'i.e.': 0.22; 'code': 0.23; 'anyone':
 0.25; 'available,': 0.26; 'object': 0.26; 'bit': 0.27; 'function':
 0.27; '>>>': 0.28; 'wrong': 0.28; 'think': 0.29; 'header:User-
 Agent:1': 0.30; 'takes': 0.31; 'module': 0.31; 'course.': 0.32;
 'extract': 0.32; 'python-list': 0.32; 'subject:Using': 0.32;
 'but': 0.32; 'received:192.168.1': 0.32; 'there': 0.33; 'header
 :In-Reply-To:1': 0.34; "didn't": 0.34; 'trying': 0.35; 'yes,':
 0.35; 'files': 0.36; "it's": 0.37; 'example': 0.37; 'others':
 0.37; 'received:192.168': 0.37; 'file': 0.38; 'way': 0.38;
 'means': 0.38; 'read': 0.38; 'use': 0.39; 'methods': 0.39;
 'data.': 0.40; 'situation': 0.40; 'want': 0.40; 'should': 0.40;
 'view': 0.60; 'method': 0.61; 'came': 0.65; 'years': 0.65;
 'that,': 0.68; 'per': 0.68; 'interested': 0.68; 'obliged': 0.69;
 'depending': 0.70; 'covered': 0.75; 'history': 0.75; 'bear': 0.76;
 'out,': 0.78; 'practical': 0.84; 'jon': 0.84; 'really.': 0.84;
 'subject: \n ': 0.84; 'subject:open': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=plus.com; s=042019;
 t=1730394372; bh=K+PYXZZkWXQmvT7l+0HYMtauLu915RBgIu9elNDjS+c=;
 h=Date:Subject:To:References:From:In-Reply-To;
 b=JgSz/v68CanU/DPa5ysnBIHBagleW+5yFbf3ln86ktzgd+8BI71cTObgr7vNRUBuq
 ozmaU1i0PGghD9AtqgNMQqqpRMbL5Z6PbqpMxb35S2D3q6HA2PbDcr5Uz94dzxBoEC
 kT99DxfnD/ms54TZKJu5LeMGECVan6MFnk7dIlIfF0Txr1u2RWyZ4912LK+hQsmMPj
 ovjqUEY3aMazwikRvi+7GQkcESEhEtXz7gQZBI0tnml5pD0zhh8DgKe/14T2ef+RCJ
 pKjEDkH17KMaY60zn9qrBIqw0fp7z9NU2qAQZHs5UefHdaxJFoRmYZWcHDrSPbmncn
 OjGH2L5/ASDew==
X-Clacks-Overhead: "GNU Terry Pratchett"
X-CM-Score: 0.00
X-CNFS-Analysis: v=2.4 cv=XaAqz555 c=1 sm=1 tr=0 ts=6723b904
 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17
 a=IkcTkHD0fZMA:10 a=6DqTnFL-AAAA:8 a=rKRx6uhQ7eibbEicGR4A:9 a=QEXdDO2ut3YA:10
 a=hx3MKrCosyGNkMWN68_U:22
X-AUTH: mrabarnett@:2500
User-Agent: Mozilla Thunderbird
Content-Language: en-GB
In-Reply-To: <87y124db0q.fsf@zedat.fu-berlin.de>
X-CMAE-Envelope: MS4xfNExME2sCK47gWbsuXDtcjmvr/DfBjsvp1oC993QAIQLdd/jJK/ICkxjMWAo+u44Etf+Aut5bpcie2ihEJAVxyiAEY3/6gYT09uIeF4i08p5j7wbAX0B
 G3UOBg2ZPUAYfpm2oWfSCpwfh8SEThi1Khr/3Y8DeAAuV61TTDhbu0g66h46cV2bE6An3QQoDQlWaHYbzUD2/sTOOHqJBbXo8Hw=
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: <a88893a0-627d-4d30-83d1-ec91b318bf99@mrabarnett.plus.com>
X-Mailman-Original-References: <87plnj3te6.fsf@zedat.fu-berlin.de>
 <slrnvi2035.372.jon+usenet@raven.unequivocal.eu>
 <87bjz1vj2c.fsf@zedat.fu-berlin.de>
 <slrnvi4ksp.372.jon+usenet@raven.unequivocal.eu>
 <87r07xtwg7.fsf@zedat.fu-berlin.de>
 <slrnvi4ss3.372.jon+usenet@raven.unequivocal.eu>
 <87y124db0q.fsf@zedat.fu-berlin.de>
Bytes: 7623

On 2024-10-31 06:47, Loris Bennett via Python-list wrote:
> Jon Ribbens <jon+usenet@unequivocal.eu> writes:
> 
>> On 2024-10-30, Loris Bennett <loris.bennett@fu-berlin.de> wrote:
>>> Jon Ribbens <jon+usenet@unequivocal.eu> writes:
>>>> On 2024-10-30, Loris Bennett <loris.bennett@fu-berlin.de> wrote:
>>>>> Jon Ribbens <jon+usenet@unequivocal.eu> writes:
>>>>>> As per the docs you link to, the read() method only takes filename(s)
>>>>>> as arguments, if you have an already-open file you want to read then
>>>>>> you should use the read_file() method instead.
>>>>>
>>>>> As you and others have pointed out, this is indeed covered in the docs,
>>>>> so mea culpa.
>>>>>
>>>>> However, whereas I can see why you might want to read the config from a
>>>>> dict or a string, what would be a use case in which I would want to
>>>>> read from an open file rather than just reading from a file(name)?
>>>>
>>>> The ConfigParser module provides read(), read_file(), read_string(),
>>>> and read_dict() methods. I think they were just trying to be
>>>> comprehensive. It's a bit non-Pythonic really.
>>>
>>> OK, but is there a common situation might I be obliged to use
>>> 'read_file'?  I.e. is there some common case where the file name is not
>>> available, only a corresponding file-like object or stream?
>>
>> Well, sure - any time it's not being read from a file. A bit ironic
>> that the method to use in that situation is "read_file", of course.
>> In my view the read() and read_file() methods have their names the
>> wrong way round. But bear in mind this code is 27 years old, and
>> the read() function came first.
> 
> Yes, I suppose history has a lot to answer for :-)
> 
> However I didn't make myself clear: I understand that there are
> different functions, depending on whether I have a file name or a
> stream.  Nevertheless, I just can't think of a practical example where I
> might just have *only* a stream, especially one containing my
> configuration data.  I was just interested to know if anyone can give an
> example.
> 
What if the config file was inside a zipped folder?

Although I haven't used ConfigParser like that, I have read the contents 
of files that are in a zipped folder. It means that I don't have to 
extract the file first.