zoukankan      html  css  js  c++  java
  • python 之configparser模块

    该模块的作用 就是使用模块中的RawConfigParser()ConfigParser()、 SafeConfigParser()这三个方法(三者择其一),创建一个对象使用对象的方法对指定的配置文件做增删改查 操作。

    1.【创建一个配置文件的方法步骤】

    import  configparser
    
    config=configparser.ConfigParser()
    #按字典的方式添加
    config["DEFAULT"]={'ServerAliceIntervale':'45',
                        'Compression':'Yes',
                        'ComperssionLevel':'9'
                       }
    config['bitbucket.org']={'User':'kebi'}
    config['topsecret.server.com']={'Host Port':'50022','ForwardXll':'no'}
    # topsecret=config['topsecret.server.com']
    # topsecret['Host Port']='50022'
    # topsecret['ForwardXll']='no'
    config['DEFAULT']['ForwardXll']='yes'
    
    #写入文件
    with open(r'example.ini','w') as configfile:
        config.write(configfile)

    【3】读取文件

    # 读取文件
    config.read('example.ini')
    print(config.sections())    #返回可用的section的列表;默认section不包括在列表中
    print(config.defaults())#    返回包含实例范围默认值的字典。
    
    print('ddddd',config.options('bitbucket.org'))#获取所有的配置表名字key
    for key in config['bitbucket.org']:#返回所有的可以,不仅仅是这个建值对下的key
        print(key)
    
    

    【4】删除,修改

    #删除
    #config.remove_section('topsecret.server.com')
    
    #config.set('bitbucket.org','age','22')#要赋值的话,赋值完要重新写入,不然不成功
    #修改
    #config.set('bitbucket.org','user','zhanmus')
    
    config.remove_option('bitbucket.org','user')#通过键值对删除
    #重新写入
    config.write(open('example.ini','w'))#修改删除都要重新写入,不然不成功

  • 相关阅读:
    java基础的判断循环
    idea反编译
    JavaWeb
    JavaWeb
    JavaWeb
    JavaWeb
    pycharm使用virtualenv环境
    django常用操作
    Linux下Tomcat的安装以及项目部署
    Linux下JDK的安装
  • 原文地址:https://www.cnblogs.com/lanyinhao/p/9158781.html
Copyright © 2011-2022 走看看