zoukankan      html  css  js  c++  java
  • python读写配置文件

    #coding:utf-8

    import ConfigParser

    class Conf():

    def __init__(self,name):
    self.name = name
    self.cp = ConfigParser.ConfigParser()
    self.cp.read(name)


    def getSections(self):
    return self.cp.sections()

    def getOptions(self, section):
    if self.cp.has_section(section):
    return self.cp.options(section)

    def getItems(self, section):
    if self.cp.has_section(section):
    return self.cp.items(section)

    def getValue(self, section, option):
    if self.cp.has_option(section, option):
    return self.cp.get(section, option)

    def setSection(self, section):
    if not self.cp.has_section(section):
    self.cp.add_section(section)
    self.cp.write(open(self.name,'w'))

    def setValue(self, section, option, value):
    if not self.cp.has_option(section, option):
    self.cp.set(section, option, value)
    self.cp.write(open(self.name,'w'))

    def delSection(self, section):
    if self.cp.has_section(section):
    self.cp.remove_section(section)
    self.cp.write(open(self.name,'w'))

    def delOption(self, section, option):
    if self.cp.has_option(section, option):
    self.cp.remove_option(section, option)
    self.cp.write(open(self.name,'w'))

    def updateValue(self, section, option, value):
    if self.cp.has_option(section, option):
    self.cp.set(section, option, value)
    self.cp.write(open(self.name,'w'))

    if __name__ == "__main__":
    conf = Conf("confx.ini")
    conf.setSection("add")
    conf.setValue("add", "version", "v1.0")
    conf.updateValue("add", "version", "v1.1")

    print conf.getItems("add")
    print conf.getSections()
    conf.delSection("add")

    #-----------------conf.ini--------------------
    #[db]
    #db_host = 127.0.0.1
    #db_port = 3306
    #db_user = root
    #db_pass = wells
    #
    #[concurrent]
    #thread = 10
    #processor = 20

  • 相关阅读:
    Redis操作命令大全
    Redis实用监控工具一览
    Redis缓存雪崩、缓存穿透、缓存击穿、缓存降级、缓存预热、缓存更新
    Redis GEO地理位置信息,查看附近的人
    详解redis持久化
    详解Supervisor进程守护监控
    详解Redis Cluster集群
    arduino使用rfid
    树莓派控制WS2812
    Arduino读取温湿度dh11+烟雾气体MQ2+彩灯ws2812
  • 原文地址:https://www.cnblogs.com/DjangoBlog/p/3529336.html
Copyright © 2011-2022 走看看