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


Groups > comp.lang.python > #196907

Re: Using 'with open(...) as ...' together with configparser.ConfigParser.read

From MRAB <python@mrabarnett.plus.com>
Newsgroups comp.lang.python
Subject Re: Using 'with open(...) as ...' together with configparser.ConfigParser.read
Date 2024-10-29 16:10 +0000
Message-ID <mailman.57.1730218436.4695.python-list@python.org> (permalink)
References <87plnj3te6.fsf@zedat.fu-berlin.de> <61f0d076-a72e-467d-b4a7-91c1c5792933@mrabarnett.plus.com>

Show all headers | View raw


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.

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


Thread

Using 'with open(...) as ...' together with configparser.ConfigParser.read "Loris Bennett" <loris.bennett@fu-berlin.de> - 2024-10-29 14:56 +0100
  Re: Using 'with open(...) as ...' together with configparser.ConfigParser.read Jon Ribbens <jon+usenet@unequivocal.eu> - 2024-10-29 15:33 +0000
    Re: Using 'with open(...) as ...' together with configparser.ConfigParser.read "Loris Bennett" <loris.bennett@fu-berlin.de> - 2024-10-30 14:03 +0100
      Re: Using 'with open(...) as ...' together with configparser.ConfigParser.read Jon Ribbens <jon+usenet@unequivocal.eu> - 2024-10-30 15:41 +0000
        Re: Using 'with open(...) as ...' together with configparser.ConfigParser.read "Loris Bennett" <loris.bennett@fu-berlin.de> - 2024-10-30 16:57 +0100
          Re: Using 'with open(...) as ...' together with configparser.ConfigParser.read Jon Ribbens <jon+usenet@unequivocal.eu> - 2024-10-30 17:57 +0000
            Re: Using 'with open(...) as ...' together with configparser.ConfigParser.read "Loris Bennett" <loris.bennett@fu-berlin.de> - 2024-10-31 07:47 +0100
              Re: Using 'with open(...) as ...' together with configparser.ConfigParser.read Jon Ribbens <jon+usenet@unequivocal.eu> - 2024-10-31 08:41 +0000
              Re: Using 'with open(...) as ...' together with configparser.ConfigParser.read Karsten Hilbert <Karsten.Hilbert@gmx.net> - 2024-10-31 17:10 +0100
              Re: Using 'with open(...) as ...' together with configparser.ConfigParser.read MRAB <python@mrabarnett.plus.com> - 2024-10-31 17:06 +0000
  Re: Using 'with open(...) as ...' together with configparser.ConfigParser.read MRAB <python@mrabarnett.plus.com> - 2024-10-29 16:10 +0000
  Re: Using 'with open(...) as ...' (Posting On Python-List Prohibited) Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-10-30 00:10 +0000

csiph-web