ConfigParser - 写入现有部分[重复]

问题描述 投票:0回答:2

我有点卡在

ConfigParser

我想向现有部分添加特定设置。

我愿意:

import ConfigParser
Config = ConfigParser.ConfigParser()
Config
Config.read("/etc/yum.repos.d/epel.repo")
Config.sections()
Config.set('epel','priority',10)
with open('/etc/yum.repos.d/epel.repo', 'w') as fout:

然后显示:

...
  File "<stdin>", line 2

    ^
IndentationError: expected an indented block
>>>
python configuration-files
2个回答
6
投票

简单来说,

   with open('epel.cfg', 'wb') as configfile:
        config.write(configfile)

请参阅此处了解示例和文档。


1
投票

您要找的方法是

Config.write

例如,参见文档中的第一个示例

它应该接受一个类似文件的对象来写入配置数据。例如:

with open('new_config.cfg', 'w') as fout:
    Config.write(fout)
© www.soinside.com 2019 - 2024. All rights reserved.