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

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

Path: ...!news.roellig-ltd.de!open-news-network.org!weretis.net!feeder8.news.weretis.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: Tue, 29 Oct 2024 16:10:47 +0000
Lines: 56
Message-ID: <mailman.57.1730218436.4695.python-list@python.org>
References: <87plnj3te6.fsf@zedat.fu-berlin.de>
 <61f0d076-a72e-467d-b4a7-91c1c5792933@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 rmQJ1HO32onG1ozw8Rc58AbxEgTJH3p2w4mKZN6ZUpdg==
Cancel-Lock: sha1:DTi6DwMdSlwUQqOyJ6aJmTwOkLo= sha256:7isa7UpUL4HG9ljUXcr2tkoLQaO/4wzh75EcCFx7scQ=
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=NZwP5Xfg;
 dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.007
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'variable': 0.05;
 'from:addr:python': 0.09; 'received:192.168.1.64': 0.09;
 'cheers,': 0.11; '(it': 0.16; 'bennett': 0.16; 'expects': 0.16;
 'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16;
 'instead.': 0.16; 'message-id:@mrabarnett.plus.com': 0.16;
 'paths': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16;
 'received:plus.net': 0.16; 'treats': 0.16; 'wrote:': 0.16;
 'python': 0.16; 'to:addr:python-list': 0.20; 'examples': 0.25;
 'object': 0.26; 'header:User-Agent:1': 0.30; 'files,': 0.32;
 'python-list': 0.32; 'subject:Using': 0.32; 'but': 0.32;
 'received:192.168.1': 0.32; 'there': 0.33; 'path': 0.33; 'header
 :In-Reply-To:1': 0.34; 'able': 0.34; 'one.': 0.35; '...': 0.37;
 "skip:' 10": 0.37; 'received:192.168': 0.37; 'file': 0.38; 'url-
 ip:151.101.0.223/32': 0.38; 'url-ip:151.101.128.223/32': 0.38;
 'url-ip:151.101.192.223/32': 0.38; 'url-ip:151.101.64.223/32':
 0.38; 'read': 0.38; 'use': 0.39; 'want': 0.40; 'try': 0.40;
 'should': 0.40; 'skip:o 20': 0.63; 'here': 0.63; 'i.e': 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=1730218248; bh=USKQpOQ02+qXb0/OteHSJsVR7sZPTi460Y+CJVpYiDw=;
 h=Date:Subject:To:References:From:In-Reply-To;
 b=NZwP5XfgdQiuWb6JFO28Vv3/rACUEyOVl0cbZFWJg8eO9ULXLPhaRkdg0wgs069zE
 E1ORHw6cGD5DTpxseeRTzwd0quiekX7wR6bDd1tu8lWuV1NCw1S29PVBLk852H3O3u
 5ZiRLdxJ3A5sz3v2OBqbRtBqyX7jgMr3SfbI/a8itj8tr3id6lcsmqCbvg69xFWGz2
 kdiWeL/3yYVEE00wAgpmB9KlwqZimkgrETOO4qlBST53ZGNyGtqSMRVjBXOA+1gFn8
 787PG2VdD/QAv9ESHM2E1Z25yvWwZcZ6dkRFrY9mpZY1NqVkRIgvy4zfVRJYlAtTfg
 vV/zwlPOEyBsw==
X-Clacks-Overhead: "GNU Terry Pratchett"
X-CM-Score: 0.00
X-CNFS-Analysis: v=2.4 cv=K5mBHDWI c=1 sm=1 tr=0 ts=67210908
 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17
 a=IkcTkHD0fZMA:10 a=8AHkEIZyAAAA:8 a=tyeaRgkwHCbBz36ZGYQA:9 a=QEXdDO2ut3YA:10
 a=FDWBRInjw0UA:10
X-AUTH: mrabarnett@:2500
User-Agent: Mozilla Thunderbird
Content-Language: en-GB
In-Reply-To: <87plnj3te6.fsf@zedat.fu-berlin.de>
X-CMAE-Envelope: MS4xfIR8nfzLru9QuGsjhmestbpVosiD5zOT98VOj5PpVjfPOyvhX3oN715x8Lxjaym0FXivEhsjsHFtAf5YkKdXV89bC2HuHq+mY+ll3OwERgbuaCIoaw38
 vt+IYt/xxdkRrEYWWw3MqlyJRrFw2wYxydVhnTGiGo7SUx5xTtiF2h6Tdm0tKO9in8qC6fufZOeOnPVoTVyem1nJPAIPP0DeD5I=
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: <61f0d076-a72e-467d-b4a7-91c1c5792933@mrabarnett.plus.com>
X-Mailman-Original-References: <87plnj3te6.fsf@zedat.fu-berlin.de>
Bytes: 5932

On 2024-10-29 13:56, Loris Bennett via Python-list wrote:
> Hi,
> 
> With Python 3.9.18, if I do
> 
>      try:
>          with open(args.config_file, 'r') as config_file:
>              config = configparser.ConfigParser()
>              config.read(config_file)
>              print(config.sections())
> 
> i.e try to read the configuration with the variable defined via 'with
> ... as', I get
> 
>     []
> 
> whereas if I use the file name directly
> 
>      try:
>          with open(args.config_file, 'r') as config_file:
>              config = configparser.ConfigParser()
>              config.read(args.config_file)
>              print(config.sections())
> I get
> 
>    ['loggers', 'handlers', 'formatters', 'logger_root', 'handler_fileHandler', 'handler_consoleHandler', 'formatter_defaultFormatter']
> 
> which is what I expect.
> 
> If I print type of 'config_file' I get
> 
>    <class '_io.TextIOWrapper'>
> 
> whereas 'args.config_file' is just
> 
>    <class 'str'>
> 
> Should I be able to use the '_io.TextIOWrapper' object variable here?  If so how?
> 
> Here
> 
>    https://docs.python.org/3.9/library/configparser.html
> 
> there are examples which use the 'with open ... as' variable for writing
> a configuration file, but not for reading one.
> 
> Cheers,
> 
> Loris
> 
'config.read' expects a path or paths. If you give it a file handle, it 
treats it as an iterable. (It might be reading the line as paths of 
files, but I haven't tested it).

If you want to read from an open file, use 'config.read_file' instead.