• 注册
当前位置:1313e > 默认分类 >正文

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'))      #此行必须有,因为文件是不可修改的,所以删除或修改操作必须是重写一次

 

转载于:https://www.cnblogs.com/Finance-IT-gao/p/10433053.html

本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 162202241@qq.com 举报,一经查实,本站将立刻删除。

最新评论

欢迎您发表评论:

请登录之后再进行评论

登录