ConfigParser是用于读取ini文件的配置内容

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
)
posted @ 2022-01-29 13:54  明天有盼望  阅读(33)  评论(0编辑  收藏  举报