import configparser
config=configparser.ConfigParser()
config[
'DEFAULT']={
'ServerAliveInterval':
'45',
'Compression':
'yes',
'CompressionLevel':
'9 '
}
config[
'bitbucket.org']={}
config[
'bitbucket.org'][
'User']=
'laowang'
config[
'myinit.com']={}
myinit=config[
'myinit.com']
myinit[
'Host']=
'127.0.0.1'
myinit[
'domain']=
'www.myinit.com'
with open(
'configs_test',
'w')
as conf:
config.
write(conf)
config_read=configparser.ConfigParser()
config_read.
read(
'configs_test')
print(
'节点:',config_read.sections())
print(
'host值:',config_read[
'myinit.com'][
'host'])
print(
"取值",config_read.
get(
'myinit.com',
'host'))
print(
'items:',config.
items(
'myinit.com'))
print(config_read.options(
'myinit.com'))
for key
in config_read[
'myinit.com']:
print(
'配置名:',key,
'值:',config_read[
'myinit.com'][key])
config_del=configparser.ConfigParser()
config_del.
read(
'configs_test')
config_del.remove_option(
'myinit.com',
'domain')
config_del.remove_section(
'myinit.com')
with open(
'configs_del',
'w')
as conf_del:
config_del.
write(conf_del)
config_add=configparser.ConfigParser()
config_add.
read(
'configs_test')
config_add.add_section(
'localhost')
config_add[
'localhost'][
'host']=
'127.0.0.1'
config_add.
set(
'localhost',
'domain',
'none')
config_add.
write(
open(
'config_add',
'w'))
config_add.
set(
'localhost',
'domain',
'test')
config_add[
'localhost'][
'domain']=
'hahaha'
config_add.
write(
open(
'config_update',
'w'))
转载请注明原文地址: https://ju.6miu.com/read-675402.html