Configuration file parsing module
Generate configuration file
configparser.ConfigParser()
instantiated object
Simulate the generation of samba configuration files, store and read in the form of a dictionary
# -*- 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)
generated configuration file
[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
query configuration file
configparser.ConfigParser.sections()
Read configuration file instance
# -*- 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()
Read the parameters of the configuration block
# -*- coding:utf8 -*-
import configparser
config = configparser.ConfigParser() #实例化对象
config.read("smb.conf") #读取配置文件
print(config.options("home")) #读取配块参数(字典中的键)
['comment', 'browseable', 'read only', 'inherit acls']
configparser.ConfigParser.items()
Read the configuration information of the configuration block (including parameters and values)
# -*- 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()
Read parameter information (parameter value) in the configuration block
# -*- coding:utf8 -*-
import configparser
config = configparser.ConfigParser() #实例化对象
config.read("smb.conf") #读取配置文件
print(config.get("home","comment")) #读取配置的参数信息(字典中的值)
This is test !!!
Modify the configuration file
configparser.ConfigParser.add_section()
Add configuration information
#-*- 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")) #追加写入配置信息
After adding configuration information
[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()
Delete the configuration information in the configuration block
#-*- coding:utf8 -*-
import configparser
config = configparser.ConfigParser() #实例化对象
config.read("smb.conf") #读取配置文件
config.remove_option("test","obj") #删除配置块配置信息
config.write(open("smb.conf","w")) #保存变更后的配置文件
Profile information after deletion
[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()
remove configuration block
#-*- 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]