#-*-Coding:utf8-*-#Auth:fulimeiImportConfigparser#First labelconf=Configparser. Configparser () conf['DEFAULT']={'Serveraliveinterval':' $', 'Compression':'Yes', 'CompressionLevel':'9'}conf['DEFAULT']['ForwardX11'] ='Yes'#a second labelconf['bitbucket.org']={}conf['bitbucket.org']['User']='HG'#a third labelconf['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)Write config file
Read a value in the configuration file
Importconfigparserconf=Configparser. Configparser () Conf.read ('Config_file.ini')Print(Conf.defaults ())#ordereddict (' compression ', ' yes '), (' CompressionLevel ', ' 9 '), (' Serveraliveinterval ', ' a '), (' forwardx11 ', ' Yes ')])Print(conf['topsecret.server.com']['Host Port'])#Delete a second labelPrint(Conf.remove_section ('bitbucket.org')) Conf.write (open ('After_file.ini','W'))#post-deletion configuration fileread a value in the configuration file
Python: Configuration file Configparser