Try extracting the section first like this:
config.get('Preferences').as_bool('delete_old_files')
質問
I have a configobj file, which I am able to read from, however I'd like to read a few of the values from the file using the as_bool method. Currently I am using the following code and failing miserably!
configFile = 'config.conf'
config = ConfigObj(configFile)
del_files_bool = config.as_bool['Preferences']['delete_old_files']
The config file itself is stuctured like this
[Prefrences]
delete_old_files = 1
Where am I going wrong?
解決
Try extracting the section first like this:
config.get('Preferences').as_bool('delete_old_files')
他のヒント
According to their documentation , as_bool takes key as an argument. This should work:
config['Preferences'].as_bool('delete_old_files')
If you have sub-sections inside sections, you could do this:
config['section']['sub-section'].as_bool('key')
It works for me in configobj version 5.0.6:
config['section1'].as_bool('key1')
config['section1'].as_int('key2')
config['section1']['sub-section'].as_float('key3')
config['section1']['sub-section'].as_list('key4')
The documentation mention these methods here.
Hope it helps!