MyConf.py
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
from configparser import ConfigParser
class MyConf(ConfigParser):
def __init__(self,filename):
super().__init__()
self.read(filename,encoding='utf-8')
比如从mysql.ini 读取配置信息
# 实例化配置类对象
conf = MyConf(os.path.join(Conf_dir, 'mysql.ini'))
# 1.数据库连接、生成游标
self.db = pymysql.connect(
host=conf.get("mysql", "host"),
user=conf.get("mysql", "user"),
password=conf.get("mysql", "passwd"),
port=conf.getint("mysql", "port"),
database=conf.get("mysql", "database"),
charset="utf8",
cursorclass=pymysql.cursors.DictCursor
)