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'))      #此行必须有,因为文件是不可修改的,所以删除或修改操作必须是重写一次
  • 相关阅读:
    Windows下通过Xmanager远程桌面控制Linux
    kk
    Wingware.WingIDE.Professional.v3.2.9.1破解并激活
    CentOS LInux启动关闭和服务管理(zt)
    Windows 7开启ping
    apache和cgi问题
    CentOS启动时自动加载内核模块
    bash shell执行、排错、启动配置文件
    程序员都应该好好想想!
    有点意思啊!
  • 原文地址:https://www.cnblogs.com/Finance-IT-gao/p/10433053.html
Copyright © 2011-2022 走看看