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

Liste des GroupesRevenir à cl python 
Sujet : Re: Using 'with open(...) as ...' together with configparser.ConfigParser.read
De : python (at) *nospam* mrabarnett.plus.com (MRAB)
Groupes : comp.lang.python
Date : 29. Oct 2024, 17:10:47
Autres entêtes
Message-ID : <mailman.57.1730218436.4695.python-list@python.org>
References : 1 2
User-Agent : Mozilla Thunderbird
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.

Date Sujet#  Auteur
29 Oct 24 * Using 'with open(...) as ...' together with configparser.ConfigParser.read12Loris Bennett
29 Oct 24 +* Re: Using 'with open(...) as ...' together with configparser.ConfigParser.read9Jon Ribbens
30 Oct 24 i`* Re: Using 'with open(...) as ...' together with configparser.ConfigParser.read8Loris Bennett
30 Oct 24 i `* Re: Using 'with open(...) as ...' together with configparser.ConfigParser.read7Jon Ribbens
30 Oct 24 i  `* Re: Using 'with open(...) as ...' together with configparser.ConfigParser.read6Loris Bennett
30 Oct 24 i   `* Re: Using 'with open(...) as ...' together with configparser.ConfigParser.read5Jon Ribbens
31 Oct 24 i    `* Re: Using 'with open(...) as ...' together with configparser.ConfigParser.read4Loris Bennett
31 Oct 24 i     +- Re: Using 'with open(...) as ...' together with configparser.ConfigParser.read1Jon Ribbens
31 Oct 24 i     +- Re: Using 'with open(...) as ...' together with configparser.ConfigParser.read1Karsten Hilbert
31 Oct 24 i     `- Re: Using 'with open(...) as ...' together with configparser.ConfigParser.read1MRAB
29 Oct 24 +- Re: Using 'with open(...) as ...' together with configparser.ConfigParser.read1MRAB
30 Oct 24 `- Re: Using 'with open(...) as ...' (Posting On Python-List Prohibited)1Lawrence D'Oliveiro

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal