简体   繁体   中英

python configparser writing to defaults section

i'm using the configparser module in python to read and write some .ini style files. i want to be able to create and write to the DEFAULTS section, however, it appears to be hardcoded to not allow the creation of such a section.

is it possible? or even advised to do this?

You don't have to create the DEFAULT section, it already exists. You can set values in it right away.

config = ConfigParser.RawConfigParser()
config.set('DEFAULT', 'name2', 'value2')
with open('file.conf', 'wb') as cf:
    config.write(cf)

The values you set as defaults when creating the ConfigParser instance will also get written to the DEFAULT section, as wim noted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM