python模块系列之 - configparser

本文介绍了如何利用Python的configparser模块来管理配置文件。详细讲述了读取、添加、删除及更新配置文件的方法,并提供了具体代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

configparser模块用来管理类似于windows下的ini文件格式的配置文件,在python2中此模块名为Configparser. 
配置文件格式如下:

config.ini

[userinfo]              -------> section
username = super
passwd = 12345
lockstatus = 0
[dbconfig]           -------------> section
host = 127.0.0.1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

文件元素描述为: 
section: 节点名,配置文件中的所有 [] 的集合 section = [‘userinfo’,’dbconfig’] 
item : 节点下的所有配置项 section[0].item = [(‘username’, ‘super’), (‘passwd’, ‘12345’),(‘lockstatus’,’0’)] 
options : item中的每一行配置的key section[0].item。options = [‘username’, ‘passwd’, ‘lockstatus’]

1 读取配置文件

import configparser

def configfile_read():
    config = configparser.ConfigParser()
    config.read("config.ini")
    # 获取所有section节点信息,返回结果为列表
    section = config.sections()
    print("Sections:  ", section)
    # 显示section[0]下的所有options信息
    print("options: ", config.options(section[0]))
    # 显示section[0]下的所有 key,value值
    print("items(section[0]): ", config.items(section[0]))    
    for k, v in config.items(section[0]):
        print("key = {0}, value = {1}".format(k, v))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

运行结果:

Sections: [‘userinfo’, ‘dbconfig’] 
options: [‘username’, ‘passwd’, ‘lockstatus’] 
items(section[0]): [(‘username’, ‘super’), (‘passwd’, ‘12345’), (‘lockstatus’, ‘0’)] 
key = username, value = super 
key = passwd, value = 12345 
key = lockstatus, value = 0

2 添加配置文件

import configparser
def configfile_add():
    config = configparser.ConfigParser()
    with open("config.ini", "a+") as f:
        config.add_section("userinfo2")
        config.set("userinfo2", "username", "peter")
        config.set("userinfo2", "passwd", "12345")
        config.set("userinfo2", "lockstatus", "0")
        config.write(f)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

运行结果: 
[userinfo2] 
username = peter 
passwd = 12345 
lockstatus = 0

3 删除配置文件

def configfile_del():
    config = configparser.ConfigParser()
    config.read("config.ini")
    # 删除一个 option
    config.remove_option("userinfo2", "lockstatus")
    # 删除 section
    config.remove_section("userinfo2")
    with open(CheckPath + "\config.ini", "w+") as f:
        config.write(f)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

删除section时,不管section下内容是否为空将整个section全部删除,如上将删除 [userinfo2]的整个信息。

4 更新配置文件

def configfile_update():
    i = 0
    config = configparser.ConfigParser()
    config.read(CheckPath + "\config.ini")
    # 获取所有 section ,返回结果为列表
    section = config.sections()

    while len(section) > i:
        if section[i] == 'userinfo2':
            print(config.options(section[i]))

            # 通过 set 方法修改信息
            config.set(section[i], "username", "修改username的值")
            with open(CheckPath + "\config.ini", "w+") as f:
                config.write(f)
            break
        i += 1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值