python configparser模块操作properties文件

版权声明:本文为博主原创文章,欢迎一起学习交流。 https://blog.csdn.net/ym01213/article/details/87347509

properties文件中定义属性

import configparser as cfp

def getProp():
    filePath = '../dao/properties/prop.properties' #配置文件的相对路径
    cf = cfp.ConfigParser()
    cf.read(filePath)
    # 获取所有section,返回值为list
    print(cf.sections())
    # 获取属性名,返回一个属性名称list
    propName = cf.options('ORACLE')
    print(propName)
    # 获取属性名与属性值对
    kv = cf.items('ORACLE')
    print(kv)
    # 获取指定属性值
    val = cf.get('ORACLE','ORACLE_HOST')
    print(val)

    # 添加配置节点
    cf.add_section('other')
    cf.set('other','name','zhangsan')
    cf.write(open(filePath,'w'))

猜你喜欢

转载自blog.csdn.net/ym01213/article/details/87347509