1、设置配置文件
[mysql]
host = 1234
port = 3306
user = root
password = Zhsy08241128
database = leartd
2、读取配置文件
import configparser
import os
conf= 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 的option
conf.write(open(root_path + '/ceshi/conf/app.conf', 'w'))
版权声明:本文为weixin_42670402原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。