python获取configparser模块的使用


使用configparser模块读取配置文件中的内容

config.ini配置文件如下:

[ServerInfo]
ServerIP = 192.168.9.95
ManagerServicePort = 8443
CryptoServicePort = 443
ServicePort = 6180
[Database]
dbHost = 192.168.9.95
dbPort = 3306
dbUser =
dbPassword =
dbName =
[CaseFile]
caseFile = Case_demo.xlsx

demo示例:

import configparser
import os
from common import getPath

#获取配置文件信息类
class getConfig():
    #初始化
    def __init__(self):
        configFilePath = os.path.join(getPath.configPath, 'config.ini')
        self.config = configparser.ConfigParser()
        self.config.read(configFilePath)
    #获取服务器信息
    def get_ServerInfo(self,name):
        value = self.config.get('ServerInfo',name)
        return value
    def get_caseName(self,name):
        value = self.config.get('CaseFile',name)
        return value

if __name__ == '__main__':
    confInfo = getConfig()
    print(confInfo.get_caseName('caseFile'))