configparser模块

configparser 是什么? 配置文件解析模块
什么是配置文件?
用于提供程序运行所需要的一些信息的文件 后缀 ini cfg
有什么用?
方便用户修改 例如超时时间

配置文件内容格式
只包括两种元素
section 分区
option 选项
一个文件可以有多个section
一个section可以有多个选项

import configparser
# 得到配置文件对象
cfg = configparser.ConfigParser()
# 读取一个配置文件
cfg.read("download.ini")

print(cfg.sections())
print(cfg.options("section1"))

print(type(cfg.get("section1","maxspeed")))
print(type(cfg.getint("section1","maxspeed")))
print(cfg.getint("section2","minspeed"))


# 修改最大速度为2048
cfg.set("section1","maxspeed","2048")

cfg.write(open("download.ini","w",encoding="utf-8"))
posted @ 2018-08-13 22:21  星牧  阅读(85)  评论(0编辑  收藏  举报