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


Groups > comp.lang.python > #196905 > unrolled thread

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

Started by"Loris Bennett" <loris.bennett@fu-berlin.de>
First post2024-10-29 14:56 +0100
Last post2024-10-30 00:10 +0000
Articles 12 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  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

#196905 — Using 'with open(...) as ...' together with configparser.ConfigParser.read

From"Loris Bennett" <loris.bennett@fu-berlin.de>
Date2024-10-29 14:56 +0100
SubjectUsing 'with open(...) as ...' together with configparser.ConfigParser.read
Message-ID<87plnj3te6.fsf@zedat.fu-berlin.de>
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

-- 
This signature is currently under constuction.

[toc] | [next] | [standalone]


#196906

FromJon Ribbens <jon+usenet@unequivocal.eu>
Date2024-10-29 15:33 +0000
Message-ID<slrnvi2035.372.jon+usenet@raven.unequivocal.eu>
In reply to#196905
On 2024-10-29, Loris Bennett <loris.bennett@fu-berlin.de> 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.

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.

[toc] | [prev] | [next] | [standalone]


#196912

From"Loris Bennett" <loris.bennett@fu-berlin.de>
Date2024-10-30 14:03 +0100
Message-ID<87bjz1vj2c.fsf@zedat.fu-berlin.de>
In reply to#196906
Jon Ribbens <jon+usenet@unequivocal.eu> writes:

> On 2024-10-29, Loris Bennett <loris.bennett@fu-berlin.de> 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.
>
> 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)?

Cheers,

Loris

-- 
This signature is currently under constuction.

[toc] | [prev] | [next] | [standalone]


#196913

FromJon Ribbens <jon+usenet@unequivocal.eu>
Date2024-10-30 15:41 +0000
Message-ID<slrnvi4ksp.372.jon+usenet@raven.unequivocal.eu>
In reply to#196912
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.

[toc] | [prev] | [next] | [standalone]


#196914

From"Loris Bennett" <loris.bennett@fu-berlin.de>
Date2024-10-30 16:57 +0100
Message-ID<87r07xtwg7.fsf@zedat.fu-berlin.de>
In reply to#196913
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?

-- 
This signature is currently under constuction.

[toc] | [prev] | [next] | [standalone]


#196915

FromJon Ribbens <jon+usenet@unequivocal.eu>
Date2024-10-30 17:57 +0000
Message-ID<slrnvi4ss3.372.jon+usenet@raven.unequivocal.eu>
In reply to#196914
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.

[toc] | [prev] | [next] | [standalone]


#196916

From"Loris Bennett" <loris.bennett@fu-berlin.de>
Date2024-10-31 07:47 +0100
Message-ID<87y124db0q.fsf@zedat.fu-berlin.de>
In reply to#196915
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.

-- 
This signature is currently under constuction.

[toc] | [prev] | [next] | [standalone]


#196917

FromJon Ribbens <jon+usenet@unequivocal.eu>
Date2024-10-31 08:41 +0000
Message-ID<slrnvi6gli.372.jon+usenet@raven.unequivocal.eu>
In reply to#196916
On 2024-10-31, 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:
>>>> 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.

That was answered the first sentence of my reply. It's a bit vague
because in most of the situations I can think of, one of the other
read_*() methods would probably be more appropriate. But again,
the history is that read_file() was added first (originally called
readfp() ) so it had to handle all cases where the data being read
was not coming from a named filesystem file - e.g. it's coming over
a Unix socket, or an HTTP request, or from a database.

It is good practice in general to provide a method that allows your
class to read data as a stream, if that is appropriate for what
you're doing, so that people aren't unnecessarily forced to load
data fully into memory or write it to a file, as well as perhaps a
convenience method thaat will read from a named file for people who
are doing that.

[toc] | [prev] | [next] | [standalone]


#196921

FromKarsten Hilbert <Karsten.Hilbert@gmx.net>
Date2024-10-31 17:10 +0100
Message-ID<mailman.60.1730391046.4695.python-list@python.org>
In reply to#196916
Am Thu, Oct 31, 2024 at 07:47:17AM +0100 schrieb Loris Bennett via Python-list:

> 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.

Apart from the fact that any data source can be made into a
file: one might have a stream of data coming in over, say,
http, as in a centralized configuration repository.

Karsten
--
GPG  40BE 5B0E C98E 1713 AFA6  5BC0 3BEA AC80 7D4F C89B

[toc] | [prev] | [next] | [standalone]


#196923

FromMRAB <python@mrabarnett.plus.com>
Date2024-10-31 17:06 +0000
Message-ID<mailman.62.1730394380.4695.python-list@python.org>
In reply to#196916
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.

[toc] | [prev] | [next] | [standalone]


#196907

FromMRAB <python@mrabarnett.plus.com>
Date2024-10-29 16:10 +0000
Message-ID<mailman.57.1730218436.4695.python-list@python.org>
In reply to#196905
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.

[toc] | [prev] | [next] | [standalone]


#196910 — Re: Using 'with open(...) as ...' (Posting On Python-List Prohibited)

FromLawrence D'Oliveiro <ldo@nz.invalid>
Date2024-10-30 00:10 +0000
SubjectRe: Using 'with open(...) as ...' (Posting On Python-List Prohibited)
Message-ID<vfrth6$1p8n1$3@dont-email.me>
In reply to#196905
On Tue, 29 Oct 2024 14:56:01 +0100, Loris Bennett wrote:

>     with open(args.config_file, 'r') as config_file:

I never bother with putting files into context managers. This is not a 
good idea for files open for writing, but even for read-only files (as 
here), CPython will close the file when the object goes out of scope 
anyway.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web