Writing comments to files with ConfigParser

后端 未结 4 1345
感动是毒
感动是毒 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 19:58

    You could also use ConfigUpdater. It has many more convenience options to update configuration files in a minimal invasive way.

    You would basically do:

    from configupdater import ConfigUpdater
    
    updater = ConfigUpdater()
    updater.add_section('DEFAULT')
    updater.set('DEFAULT', 'test', 1)
    updater['DEFAULT']['test'].add_before.comment('test comment', comment_prefix=';')
    with open('./config.ini', 'w') as f:
        updater.write(f)
    

提交回复
热议问题