Python's Configparser module

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.