ini配置文件如下:conf.ini

[addr]
remote_url = http://127.0.0.1


[name]
name = 'saggitarxm'

test.py

from configparser import ConfigParser


def read_ini(conf_path):
    online = ConfigParser()
    online.read(conf_path)
    return online

def ini_update(conf_path, section, name, value):
    conf = ConfigParser()
    conf.read(conf_path)
    conf.set(section, name, value)
    conf.write(open(conf_path, "w"))


def ini_add_section(conf_path, section, name, value):
    conf = ConfigParser()
    conf.read(conf_path)
    conf.add_section(section)
    conf.set(section, name, value)
    conf.write(open(conf_path, "w"))


if __name__ == "__main__":
    res = read_ini('./conf.ini')
    print(res)
    print(res.get('name', 'name'))
    print(res.get('addr', 'remote_url'))

运行:

python test.py

 输出:

<backports.configparser.ConfigParser object at 0x7f3a7f7ee0d0>
'saggitarxm'
http://127.0.0.1

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐