#-*- coding:utf8 -*- # Auth:fulimei import configparser #第一个标签 conf=configparser.ConfigParser() conf['DEFAULT']={'ServerAliveInterval': '45', 'Compression': 'yes', 'CompressionLevel': '9'} conf['DEFAULT']['ForwardX11'] = 'yes' #第二个标签 conf['bitbucket.org']={} conf['bitbucket.org']['User']='hg' #第三个标签 conf['topsecret.server.com']={} res=conf['topsecret.server.com']['Host Port']='50022' res=conf['topsecret.server.com']['ForwardX11']='no' with open('config_file.ini','w') as configer: conf.write(configer)
读配置文件里的某一个值
import configparser conf=configparser.ConfigParser() conf.read('config_file.ini') print(conf.defaults()) #OrderedDict([('compression', 'yes'), ('compressionlevel', '9'), ('serveraliveinterval', '45'), ('forwardx11', 'yes')]) print(conf['topsecret.server.com']['host port']) #删除第二个标签 print(conf.remove_section('bitbucket.org')) conf.write(open('after_file.ini','w')) #删除后的配置文件