配置文件解析模块code
实例化对象对象
模拟生成samba配置文件,以字典形式存储和读取rem
# -*- 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)
生成的配置文件get
[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
读取配置文件实例it
# -*- coding:utf8 -*- import configparser config = configparser.ConfigParser() #实例化对象 config.read("smb.conf") #读取配置文件 print(config.sections()) #读取配置文件配置块 print(config['home']['comment']) #读取其中一个配置信息
['global', 'home'] #取出的配置文件块 This is test !!! #取出的一个配置信息
读取配置块的参数io
# -*- coding:utf8 -*- import configparser config = configparser.ConfigParser() #实例化对象 config.read("smb.conf") #读取配置文件 print(config.options("home")) #读取配块参数(字典中的键)
['comment', 'browseable', 'read only', 'inherit acls']
读取配置块的配置信息(包含参数和值)test
# -*- 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')]
读取配置块中的参数信息(参数的值)import
# -*- coding:utf8 -*- import configparser config = configparser.ConfigParser() #实例化对象 config.read("smb.conf") #读取配置文件 print(config.get("home","comment")) #读取配置的参数信息(字典中的值)
This is test !!!
增长配置信息配置
#-*- 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")) #追加写入配置信息
新增后配置信息coding
[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
删除配置块中配置信息
#-*- 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]
删除配置块
#-*- 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]