zoukankan      html  css  js  c++  java
  • configparser配置文件模块

    创建配置文件:

    import configparser
    config = configparser.ConfigParser()
    config['DEFAULT'] = {'server':'45',
                         'compression':'yes',
                         'compressionLevel':'9'}
    config['bucket.org'] = {'User':'hg',
                            'age':'23'}
    config['top'] = {'host':'522',
                     'forward':'no'}
    config.write(open('example.ini','w'))       #用config给example.ini里写东西
    # with open('example.ini','w') as configfile: #同上
    #     config.write(configfile)

    查看配置文件内容:

    config.read('example.ini')         #读配置文件,先读才能进行以下打印
    
    print(config.sections())           #打印默认块以外的其他块名
    print(config.default_section)      #打印默认块的名字
    print(config.defaults())           #打印默认块的内容
    print(config['bucket.org']['user'])#打印user的内容
    for key in config:                   #打印配置文件中各个块名
        print(key)
    for key in config['bucket.org']:   #打印'bucket.org'块所含选项的名字
        print(key)

    删除/修改操作:

    config.read('example.ini')                 #先读到才能进行删除或修改操作
    
    config.remove_option('bucket.org','age')   #删掉budget.org块中的age项
    config.remove_section('top')               #删掉top块
    print(config.has_section('bucket.org'))    #判断是否包含这个块
    config.set('bucket.org','user','app')      #修改budget.org块中user项的值
    
    config.write(open('example.ini','w'))      #此行必须有,因为文件是不可修改的,所以删除或修改操作必须是重写一次
  • 相关阅读:
    Linux shell 常用命令记录
    [Bat]批量重命名文件
    【转】java术语(PO/POJO/VO/BO/DAO/DTO)
    TesseractOCR3.0语言库训练步骤
    关于Delphi Rtti对应TDatetime的一些启示
    Ubuntu Server 12.04 安装mysql 5.6.10
    Delphi透明 圆角 窗体 【转】
    奇怪的~用法
    很有用的VS2005插件——SlickEdit
    还不习惯Office 2007
  • 原文地址:https://www.cnblogs.com/Finance-IT-gao/p/10433053.html
Copyright © 2011-2022 走看看