Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: "Loris Bennett" Newsgroups: comp.lang.python Subject: Using 'with open(...) as ...' together with configparser.ConfigParser.read Date: Tue, 29 Oct 2024 14:56:01 +0100 Organization: FUB-IT, Freie =?utf-8?Q?Universit=C3=A4t?= Berlin Lines: 51 Message-ID: <87plnj3te6.fsf@zedat.fu-berlin.de> Mime-Version: 1.0 Content-Type: text/plain X-Trace: news.uni-berlin.de dQj9cRxdi+Ve6mXTTXiheAmEEnDsu/kKC+DtIDN/0y2HMW Cancel-Lock: sha1:Yd/PsZWFHRLGDtGBbOGnNbTw1LY= sha1:pO9hnex7f7wgeRmzMpzGOuybeWc= sha256:iSJCuAJmZCy1FheGqTslKIHtys2xG7090FzU/RKCpvs= User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux) Xref: csiph.com comp.lang.python:196905 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 whereas 'args.config_file' is just 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.