1、设置配置文件
[mysql]host = 1234port = 3306user = rootpassword = Zhsy08241128database = leartd
2、读取配置文件
import configparserimport osconf= configparser.ConfigParser()def readConf():'''读取配置文件'''root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))conf.read(root_path + '/ceshi/conf/app.conf') # 文件路径print(conf)name = conf.get("mysql", "host") # 获取指定section 的option值print(name)
3、写入配置文件
def writeConf():'''写入配置文件'''root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))conf.read(root_path + '/ceshi/conf/app.conf') # 文件路径conf.set("mysql", "host", "1234") # 修改指定section 的optionconf.write(open(root_path + '/ceshi/conf/app.conf', 'w'))