An. ini file consists of several sections (parts), and each section consists of several key-value pairs.
Take Example.ini as an example:
[DEFAULT] serveraliveinterval = Compression = Yes compressionlevel = 9 ForwardX11 = yes[bitbucket.org] User = hg[topsecret.server.com] Port = 50022 ForwardX11 = No
Create an. ini file
ImportConfigparser#CONFIGD objects are similar to dictionariesConfig =Configparser. Configparser () config["DEFAULT"] = { "Serveraliveinterval": 45, "comperssion":"Yes", "Comperssionlevel"8 {, "ForwardX11":"Yes"}config["bitbucket.org"] ={}config["bitbucket.org"]["User"] ="HG"config["topsecret.server.com"] ={}config["topsecret.server.com"]["Port"] ="50022"config["topsecret.server.com"]["ForwardX11"] ="No"With Open ("Example.ini","W") as FP:#Write FileConfig.write (FP)
Read the. ini file
Config = Configparser. Configparser () config.read ("example.ini")
About Section
ImportConfigparserconfig=Configparser. Configparser () Config.read ("Example.ini")#get the section name listPrint(Config.sections ())#[' bitbucket.org ', ' topsecret.server.com ']#get the default section namePrint(config.default_section)#DEFAULT#get the default section objectPrint(Config.defaults ())#ordereddict (' Serveraliveinterval ', ' a '), (' comperssion ', ' yes '), (' Comperssionlevel ', ' 9 '), (' forwardx11 ', ' Yes ')])#determine if the corresponding section is includedPrint('bitbucket.org' inchconfig)Print(Config.has_section ("example.com"))#2 ways to add a sectionconfig['example.com'] ={}config['example.com']['username'] ='python'Config.add_section ("example.com1") config.write (open ("Tmp.ini","W"))#2 ways to delete a sectiondelconfig['example.com']config.remove_section ("example.com") config.write (open ("Tmp.ini","W"))
About option
ImportConfigparserconfig=Configparser. Configparser () Config.read ("Example.ini")#the option here corresponds to key#get the key list for section correspondingBitbucket_keys = Config.options ('bitbucket.org')Print(Bitbucket_keys)#[' User ', ' serveraliveinterval ', ' comperssion ', ' comperssionlevel ', ' forwardx11 ']#determine if there is a corresponding key in the sectionPrint(Config.has_option ("bitbucket.org","User"))Print('User1' inchConfig.options ('bitbucket.org'))Print('User1' inchconfig['bitbucket.org'])#Add Optionconfig['bitbucket.org']['pwd'] ='python'config.write (Open ("Tmp.ini","W"))#2 ways to remove optiondelconfig['bitbucket.org']['User']config.remove_option ("bitbucket.org","User") config.write (open ("Tmp.ini","W"))
Value related
ImportConfigparserconfig=Configparser. Configparser () Config.read ("Example.ini")#2 ways to get the value of keyuser = config['bitbucket.org']['User']user= Config.get ('bitbucket.org','User')Print(user)#HGinterval= Config.getint ("topsecret.server.com","Port")Print(interval)# $Compression = Config.getboolean ("topsecret.server.com","ForwardX11")Print(compression)#and Config.getfloat ()#2 ways to set valueConfig.set ('bitbucket.org','User','Peter') config['bitbucket.org']["User"] ="Peter"config.write (Open ("Tmp.ini","W"))
Python configparser use