import configparser
import os
class ReadConfig:
    """定义一个读取配置文件"""
    def __init__(self, filepath=None):
        if filepath:
            configpath = filepath
        else:
            root_dir = os.path.dirname(os.path.abspath('.')) # 获取上一级目录
            configpath = os.path.join(root_dir, r"config\config.ini") # 获取配置文件目录
        self.cf = configparser.ConfigParser()
        self.cf.read(configpath,encoding='utf-8')
    def get_db(self, sec, param):
        value = self.cf.get(sec, param)
        return value
    def get_testdatadir(self,param):
        testdatadir = self.cf.get("TESTDATA_DIR", param)
        return testdatadir
if __name__ == '__main__':
    test = ReadConfig()
    t = test.get_db("HTWBDB","ip")
    testdata = test.get_testdatadir("testdatadir")
    #t2 = test.get_dbhtwb('user')
    print(testdata)
config.ini文件配置
![]()