美文网首页
python读取Properties配置文件

python读取Properties配置文件

作者: 吴国友 | 来源:发表于2019-02-21 17:16 被阅读0次
    #读取Properties 文件类
    class Properties:
        fileName = ''
        def __init__(self, fileName):
            self.fileName = fileName
    
        def getProperties(self):
            try:
                pro_file = open(self.fileName, 'r',encoding='utf-8')
                properties = {}
                for line in pro_file:
                    if line.find('=') > 0:
                        strs = line.replace('\n', '').split('=')
                        properties[strs[0]] = strs[1]
            except Exception as e:
                raise e
            else:
                pro_file.close()
            return properties
    

    调用(properties['packagename'])中的key要为小写):

     url = cfg_path + '\\' + filename + '\\cfg.properties'
            properties = Properties(url).getProperties()
            print(properties['packagename'])
    

    相关文章

      网友评论

          本文标题:python读取Properties配置文件

          本文链接:https://www.haomeiwen.com/subject/yzrdyqtx.html