zoukankan      html  css  js  c++  java
  • python之模块分类(四)

    ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section), 每个节可以有多个参数(键=值)。使用的配置文件的好处就是不用在程序员写死,可以使程序更灵活。

    mysql的默认配置文件就是这样的格式:

    [mysqld]
    datadir=/var/lib/mysql
    socket=/var/lib/mysql/mysql.sock
    # Disabling symbolic-links is recommended to prevent assorted security risks
    symbolic-links=0
    # Settings user and group are ignored when systemd is used.
    # If you need to run mysqld under a different user or group,
    # customize your systemd unit file for mariadb according to the
    # instructions in http://fedoraproject.org/wiki/Systemd
    
    [mysqld_safe]
    log-error=/var/log/mariadb/mariadb.log
    pid-file=/var/run/mariadb/mariadb.pid
    
    #
    # include all files from the config directory
    #
    !includedir /etc/my.cnf.d
    

    一、写

    如果想用python生成一个文档怎么做呢?

    #Author:Anliu
    import configparser
    config = configparser.ConfigParser()
    config["DEFAULT"] = {'server_host':"DNS01",
                         'server_ip':"0.0.0.0",
                         'server_conf':"/etc/dns/name.conf"
                         }
    config["user"] = {}
    config["user"]["name"] = 'root'
    
    config["pass"] = {}
    top_s = config["pass"]
    top_s["password"] = "123456"
    top_s["pass_key"] = "/root/.ssh/key.pub"
    
    config['DEFAULT']["server_socket"] = "/lib/dns.sock"
    
    with open("named.conf","w") as configfile:
        config.write(configfile)

    结果:

    [DEFAULT]
    server_host = DNS01
    server_ip = 0.0.0.0
    server_conf = /etc/dns/name.conf
    server_socket = /lib/dns.sock
    
    [user]
    name = root
    
    [pass]
    password = 123456
    pass_key = /root/.ssh/key.pub

    二、读

    #Author:Anliu
    import configparser
    
    config = configparser.ConfigParser()
    #print(config.sections())
    config.read("named.conf")
    print(config.sections())
    if 'DEFAULT' in config:
    #    print(config['DEFAULT'])
        print(config['DEFAULT']["server_ip"])
    else:
        print("NULL")
    
    for key in config:
        print(key)

    三、增删改查

    #Author:Anliu
    import configparser
    config = configparser.ConfigParser()
    config.read("i.cfg")
    #print(f1)
    
    ###############查###############
    #查看所有节点
    secs = config.sections()
    print(secs)
    #查看节点下的键
    options = config.options("section2")
    print(options)
    #查看节点下键的值
    #val = config.get("section2","k1")
    #print(val)
    #查看节点下键的值,并将其转为整型
    #val =config.getint("section2","k1")
    
    ###############删#################
    #删除节点
    #secs = config.remove_section("section1")
    #config.write(open("i.cfg","w"))
    #删除键值
    #config.remove_option("section2","k1")
    #config.write(open("i.cfg","w"))
    
    ###############增#################
    if not config.has_section("mysql"):
        #添加节点
        config.add_section("mysql")
        config.write(open("i.cfg","w"))
    else:
        print("node is exixt...")
    
    #在node下增加键值
    config.set("mysql","path","/opt/data")
    config.write(open("i.cfg","w"))
    
    ################改#################
    #无则添加,有则修改
    config.set("mysql","path","/opt/program/data")
    config.write(open("i.cfg","w"))

     

  • 相关阅读:
    1109. Conference 夜
    世界第五大软件商Sage收购中国合作伙伴,及其建筑行业解决方案。
    中小企业ERP美国上演三国演义(微软、Sage、Intuit)
    国内推广微软 Dynamics SL (Solomon)的公司__北京AIT(爱尔的)公司不再推广
    微软的ERP路线图 The Greening of Microsoft (BY Robert L. Mitchell, IDG News Service,09/03/2005 16:50:01)
    ERP企业兼并重组将延续(Sage收购Timberline Software公司)—旧闻
    微软ERP Dynamics SL,真正的施工企业ERP也,适合工程承包商!
    来自国外(美国)的施工企业(承包商)管理软件比较和选择建议 !
    施工企业信息化咨询,我们可以找谁? (续1)
    来自 Ambient Consulting Group 两次回信。
  • 原文地址:https://www.cnblogs.com/anttech/p/12713849.html
Copyright © 2011-2022 走看看