Python 模块-configparser

一,新增配置文件

import configparser

config = configparser.ConfigParser()

config["DEFAULT"] = {'Server': "45", 'Comp': "yes"}

config['bitbuckket.org'] = {}

config['bitbuckket.org']['User'] = 'hg'

with open('data/example.ini', 'w') as configfile:
    config.write(configfile)
View Code

二、读取配置文件

import configparser

config = configparser.ConfigParser()
config.read('data/example.ini')
print(config.defaults())
print(config.sections())

sec = config.remove_section('bitbuckket.org')
config.write(open('data/example.ini',
                  "w"))
View Code
原文地址:https://www.cnblogs.com/Linc2010/p/8598390.html