创建配置文件
import configparser
def createConfig(path):
config=configparser.ConfigParser()
config.add_section("Settings")
config.set("Settings","font","Consoles")
config.set("Settings","font_size","10")
config.set("Settings", "font_style", "Normal")
with open(path,"w") as config_file:
config.write(config_file)
if __name__ == '__main__':
path="settings.ini"
createConfig(path)
读取、更新、删除配置文件中的选项
import configparser
import os
def crudConfig(path):
if not os.path.exists(path):
createConfig(path)
config=configparser.ConfigParser()
config.read(path)
font=config.get("Settings","font")
font_size=config.get("Settings","font_size")
print(font)
print(font_size)
config.set("Settings","font_size","12")
config.remove_option("Settings","font_style")
with open(path,"w") as config_file:
config.write(config_file)
if __name__ == '__main__':
path="settings.ini"
crudConfig(path)