Tag: read serve import def localhost mode port. Section Read Configuration
The Configparser module is used to generate and modify common configuration documents, and the name of the current module is changed to Configparser in the Python 3.x version.
Sample configuration file
= 2202 = 9 = = = 8080=
1. Generate Configuration file
ImportConfigparserconfig=Configparser. Configparser () config["DEFAULT"] = { 'Serveraliveinterval': 45, 'Compression':'Yes', 'CompressionLevel': 9}config['test1.net.cn'] = Dict ()#make a piece of configuration equal to a dictionaryconfig['test1.net.cn']['User'] ='Test' #add a row of configurations using key-value pairsconfig['test1.net.cn']['Host'] ='localhost'config['test1.net.cn']['Port'] ='8080' #all the values are passed in a string.config['top.net.cn'] =dict () Top= config['top.net.cn']top['Test_port'] ='9090'top['User'] ='Root'top['Port'] ='2202'With Open ('Test.ini','W') as Configfile:config.write (configfile)#Finally, be sure to write to the configuration file
#####################
[DEFAULT]
Serveraliveinterval = 45
Compression = yes
CompressionLevel = 9
[test1.net.cn]
user = Test
host = localhost
Port = 8080
[top.net.cn]
Test_port = 9090
user = root
Port = 2202
#####################
2. Read the configuration file
ImportConfigparserconfig=Configparser. Configparser () Config.read ('Test.ini')Print('top.net.cn' inchConfig#determine if a sections is in the configuration file forKeyinchConfig#Traverse all the sections Print(Key)Print(Config.sections ())#Print all the sections directlyPrint(Config.defaults ())#get the special method of default, get that individual sections, will have the value of Defalut. Print(config['test1.net.cn']['Port'])#to get a single recordPrint(Config.items ('top.net.cn'))#get a record of a sectionsPrint(Config.options ('test1.net.cn'))#gets the sections of the test1.net.cn, which also includes the value of DefalutPrint(Config.items ('top.net.cn'))#get all records for top.net.cnvar = config.get ('top.net.cn','Test_port')#gets the value of a key under a sectionsPrint(Var)#True#DEFAULT#test1.net.cn#top.net.cn#[' test1.net.cn ', ' top.net.cn ']#ordereddict ([' Serveraliveinterval ', ' a '), (' Compression ', ' yes '), (' CompressionLevel ', ' 9 ')])#8080#[(' Serveraliveinterval ', ' "'), (' Compression ', ' yes '), (' CompressionLevel ', ' 9 '), (' Test_port ', ' 9090 '), (' User ', ' Root '), (' Port ', ' 2202 ')]#[' User ', ' host ', ' Port ', ' serveraliveinterval ', ' compression ', ' compressionlevel ']#[(' Serveraliveinterval ', ' "'), (' Compression ', ' yes '), (' CompressionLevel ', ' 9 '), (' Test_port ', ' 9090 '), (' User ', ' Root '), (' Port ', ' 2202 ')]#9090
3. Modify the configuration file
ImportConfigparserconfig=Configparser. Configparser () Config.read ('Test.ini')#Delete the sections top.net.cn that already exists. Remove_sections = Config.remove_section ('top.net.cn')#If a sections exists, an exception will occur if you add it again. So I'm going to delete it and then add it. #add_section = config.has_section (' Leslie ')Config.remove_section ('Leslie')#add a sections that does not exist. Add_section = Config.add_section ('Leslie')#adds a record to an already existing sections. Config.set ('Leslie','name','Xikang')#Delete a record that is already in sections. Config.remove_option ('test1.net.cn','Port')#Finally, make sure to write these changes to the file. Config.write (Open ('Test.ini','W'))
Python configparser module