首页 > 技术文章 > python 之 configparser 模块

lixinliang 2018-03-10 11:38 原文

[root@linag python]# vim config.py

import configparser

config = configparser.ConfigParser()
config["DEFAULT"] = {'ServerAliveInterval': '45',
                     'Compression': 'yes',
                     'CompressionLevel': '9'}
config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'Root'

config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Host Port'] = '5200'
topsecret['Forward'] = 'yes'

config['DEFAULT']['Forward'] = 'No'

with open('example.ini','w') as configfile:
  config.write(configfile)

config.set('topsecret.server.com','Hosr Port','5601')  #

config.remove_section('topsecret.server.com') #
config.write(open ('xml.ini','w'))

[root@linag python]# python config.py


[root@linag python]# cat example.ini
[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9
forward = No

[bitbucket.org]
user = Root

[topsecret.server.com]
host port = 5200
forward = yes

[root@linag python]# cat xml.ini
[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9
forward = No

[bitbucket.org]
user = Root

 

推荐阅读