python中configParser模块的基础用法

来源:互联网 发布:网络男女对唱歌曲大全 编辑:程序博客网 时间:2024/06/05 09:54
#配置文件模块import configparserconfig = configparser.ConfigParser()config['default'] = {    'ServerAliveInterval': '45',    'Conpression': 'yes',    'ConpressionLevel': '9'}config['bitbucket.org'] = {    'User': 'hg'}config['topscret.server.com'] = {}topscret = config['topscret.server.com']topscret['Host Port'] = '5001'topscret['forwardX11'] = 'no'config['default']['forwarX11'] = 'yes'with open('example.ini', 'w') as configfile:    config.write(configfile)================================================================================================
#配置文件模块import configparserconfig = configparser.ConfigParser()config.read('example.ini')print(config.sections()) #['default', 'bitbucket.org', 'topscret.server.com']print(config['default']['Conpression'])for key in config['default']:    print(key)
================================================================================================

#配置文件模块import configparserconfig = configparser.ConfigParser()config.read('example.ini') #***先读取print(config.has_section('default'))config.set('bitbucket.org', 'user', 'leo')
config.remove_option('bitbucket.org', 'user')
config.write(open('example.ini', 'w')) #***重写