Sysop: | Amessyroom |
---|---|
Location: | Fayetteville, NC |
Users: | 42 |
Nodes: | 6 (0 / 6) |
Uptime: | 00:48:42 |
Calls: | 220 |
Calls today: | 1 |
Files: | 824 |
Messages: | 121,521 |
Posted today: | 6 |
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
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.
with open(args.config_file, 'r') as config_file:
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.
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)?
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.
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?
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.
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.
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.
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.