Python模块-configparser模块

配置文件解析模块

生成配置文件

configparser.ConfigParser()

实例化对象

模拟生成samba配置文件,以字典形式存储和读取

# -*- coding:utf8 -*-
import configparser
config = configparser.ConfigParser()                #实例化对象
config["global"] = {                                #配置信息
                   'workgroup':'SAMBA',
                   'security':'user',
                   'passdb backend':'tdbsam',
                   'printing':'cups',
                   'printcap name':'cups',
                   'load printers':'yes',
                   'cups options':'raw',
                    }
config["home"] = {                                  #配置信息
                    'comment':'This is test !!!',
                    'browseable':'No',
                    'read only':'No',
                    'inherit acls':'Yes'
                    }
with open("smb.conf","w") as smb_config:            #将配置信息写入文件
    config.write(smb_config)

生成的配置文件

[global]
workgroup = SAMBA
security = user
passdb backend = tdbsam
printing = cups
printcap name = cups
load printers = yes
cups options = raw

[home]
comment = This is test !!!
browseable = No
read only = No
inherit acls = Yes

查询配置文件

configparser.ConfigParser.sections()

读取配置文件实例

# -*- coding:utf8 -*-
import configparser
config = configparser.ConfigParser()    #实例化对象

config.read("smb.conf")                 #读取配置文件
print(config.sections())                #读取配置文件配置块
print(config['home']['comment'])        #读取其中一个配置信息
['global', 'home']                      #取出的配置文件块
This is test !!!                        #取出的一个配置信息

configparser.ConfigParser.option()

读取配置块的参数

# -*- coding:utf8 -*-
import configparser
config = configparser.ConfigParser()    #实例化对象

config.read("smb.conf")                 #读取配置文件
print(config.options("home"))           #读取配块参数(字典中的键)
['comment', 'browseable', 'read only', 'inherit acls']

configparser.ConfigParser.items()

读取配置块的配置信息(包含参数和值)

# -*- coding:utf8 -*-
import configparser
config = configparser.ConfigParser()    #实例化对象

config.read("smb.conf")                 #读取配置文件
print(config.items("home"))             #读取配置文件参数对(字典中的键和值)
[('comment', 'This is test !!!'), ('browseable', 'No'), ('read only', 'No'), ('inherit acls', 'Yes')]

configparser.ConfigParser.get()

读取配置块中的参数信息(参数的值)

# -*- coding:utf8 -*-
import configparser
config = configparser.ConfigParser()    #实例化对象

config.read("smb.conf")                 #读取配置文件
print(config.get("home","comment"))     #读取配置的参数信息(字典中的值)
This is test !!!

修改配置文件

configparser.ConfigParser.add_section()

增加配置信息

#-*- coding:utf8 -*-
import configparser
config = configparser.ConfigParser()    #实例化对象
config.read("smb.conf")                 #读取配置文件
config.add_section('test')              #添加新的配置块
config.set("test","obj","1")            #新配置块添加配置
config.write(open("smb.conf","w"))      #追加写入配置信息

新增后配置信息

[global]
workgroup = SAMBA
security = user
passdb backend = tdbsam
printing = cups
printcap name = cups
load printers = yes
cups options = raw

[home]
comment = This is test !!!
browseable = No
read only = No
inherit acls = Yes

[test]
obj = 1

configparser.ConfigParser.remove_option()

删除配置块中配置信息

#-*- coding:utf8 -*-
import configparser
config = configparser.ConfigParser()    #实例化对象
config.read("smb.conf")                 #读取配置文件

config.remove_option("test","obj")      #删除配置块配置信息
config.write(open("smb.conf","w"))      #保存变更后的配置文件

删除后配置文件信息

[global]
workgroup = SAMBA
security = user
passdb backend = tdbsam
printing = cups
printcap name = cups
load printers = yes
cups options = raw

[home]
comment = This is test !!!
browseable = No
read only = No
inherit acls = Yes

[test]

configparser.ConfigParser.remove_section()

删除配置块

#-*- coding:utf8 -*-
import configparser
config = configparser.ConfigParser()    #实例化对象
config.read("smb.conf")                 #读取配置文件

config.remove_section("home")           #删除配置块(配置块中信息清空)
config.write(open("smb.conf","w"))      #保存变更后的配置文件
[global]
workgroup = SAMBA
security = user
passdb backend = tdbsam
printing = cups
printcap name = cups
load printers = yes
cups options = raw

[test]

猜你喜欢

转载自my.oschina.net/zhaojunhui/blog/1790504