ini文件用于存储数据
ini文件中,每个文件以节点[section]开始 选项=选项值结尾
section节点不可以重名
二.ini文件的操作
import configparser 实现ini文件的操作
import configparser
def read_ini():
config=configparser.ConfigParser()
config.read("confgig.ini")
#读取section
sections=config.sections()
print(f"文件所有的节点{sections}")
for section in sections:
options=config.options(section)
print(f"{section}的节点{options}")
for option in options:
value=config.get(section,option)
print(f"{section}节点{option}选项的值是{vaulue}")
def edit_ini(filename):
config=configparser.ConfigParser()
with open(filenane) as file:
#添加节点
config.add_section("offer")
#添加选项及选项值
config.set(section="offer",option="offer01",value=20000)
config.set(sectiom="offer",option="offer02",value=25000)
#移除选项
config.remove_option(section="offer","offer01")
#移除节点
config.remove_section("offer“)
#保存文件
config.write(file)