(kfzdingoa) ➜ temp python demo_ini.py demo.ini sections: ['global', 'database'] has section?: True global section items: [('site_name', 'colinspace website'), ('site_url', 'http://blog.colinspace.com'), ('site_keywords', "['colinspace', 'devops']")] global section options: ['site_name', 'site_url', 'site_keywords'] database section has mysql_user option? True get mysql_user value from database section: demouser ==>Try to write some configurations into ini file<==
# 读取 ini 相关配置 withopen(ini_file, 'a') as f: cf.read_file(f) print("sections: ", cf.sections()) print("has section?: ", cf.has_section("global")) print("global section items: ", cf.items("global")) print("global section options: ", cf.options("global")) print("database section has mysql_user option? ", cf.has_option("database", "mysql_user")) print("get mysql_user value from database section: ", cf.get("database", "mysql_user"))
# 进行配置写入 ini 文件 print("==>Try to write some configurations into ini file<==") cf["cache"] = {"redis_host": "192.168.1.11", "redis_port": 6379} withopen(ini_file, 'w') as f: cf.write(f)