python的ConfigParser模块笔记

eg:生成配置文件

import configparser
from builtins import *

config = configparser.ConfigParser()
config['DEFAULT']={'compression':'yes',
        'serveraliveinterval':'45',
        'compressionlevel':9,
        'forwardx11':'yes'}
config['bitbucket.org']={'user':'hd'}
config['topsecret.server.com']={'host port':50022,
                'forwardx11':'no'}
with open('example.ini','w') as configfile:
config.write(configfile)

 读取:

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

移除section
config = configparser.ConfigParser()
config.read('example.ini')
config.remove_section('topsecret.server.com')
config.write(open('example2.ini','w'))
 
posted on 2021-01-26 09:30  bruce.sharp  阅读(59)  评论(0)    收藏  举报