The function of the module is to use the module RawConfigParser() , ConfigParser() and SafeConfigParser() these three methods (three to choose one), create an object to use the method of the object to the specified configuration file to do additions and deletions to change the operation.
1. "Method steps for creating a configuration file"
ImportConfigparserconfig=Configparser. Configparser ()#Add as a dictionaryconfig["DEFAULT"]={'Serveraliceintervale':' $', 'Compression':'Yes', 'Comperssionlevel':'9'}config['bitbucket.org']={'User':'Kebi'}config['topsecret.server.com']={'Host Port':'50022','Forwardxll':'No'}#topsecret=config[' topsecret.server.com ']#topsecret[' Host Port ']= ' 50022 '#topsecret[' forwardxll ']= ' no 'config['DEFAULT']['Forwardxll']='Yes'#Write FileWith open (r'Example.ini','W') as Configfile:config.write (configfile)
"3" Read file
#Read FileConfig.read ('Example.ini')Print(Config.sections ())#returns a list of available sections; The default section is not included in the listPrint(Config.defaults ())#returns a dictionary that contains the default values for the instance scope. Print('ddddd', Config.options ('bitbucket.org'))#get all the configuration table names key forKeyinchconfig['bitbucket.org']:#return all that is possible, not just the key under the built-in value Print(key)
"4" Delete, modify
#Delete#config.remove_section (' topsecret.server.com ')#config.set (' bitbucket.org ', ' age ', ' a ') #要赋值的话, assignment to be re-written, otherwise unsuccessful#Modify#config.set (' bitbucket.org ', ' user ', ' zhanmus ')Config.remove_option ('bitbucket.org','User')#Delete by key value pair#re-writeConfig.write (Open ('Example.ini','W'))#Modify Delete to be re-written or unsuccessful
Python's Configparser module