Writing comments to files with ConfigParser

后端 未结 4 1355
感动是毒
感动是毒 2020-12-30 19:26

How can one write comments to a given file within sections?

If I have:

import ConfigParser
with open(\'./config.ini\', \'w\') as f:
    conf = Config         


        
4条回答
  •  生来不讨喜
    2020-12-30 20:03

    You can create variable that starts by # or ; character:

    conf.set('default_settings', '; comment here', '')
    conf.set('default_settings', 'test', 1)
    

    created conf file is

        [default_settings]
        ; comment here = 
        test = 1
    

    ConfigParser.read function won't parse first value

    config = ConfigParser.ConfigParser()
    config.read('config.ini')
    print config.items('default_settings')
    

    gives

    [('test','1')]
    

提交回复
热议问题