python configparser写入默认部分

yee*_*379 5 python configparser

我在python中使用configparser模块来读写一些.ini样式文件.我希望能够创建和写入该DEFAULTS部分,但是,似乎硬编码不允许创建这样的部分.

可能吗?甚至建议这样做?

Bel*_*eli 5

您不必创建DEFAULT部分,它已经存在.您可以立即在其中设置值.

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

创建ConfigParser实例时,您设置为默认值的值也将被写入DEFAULT部分,如Wim所述.