Python Configparser Module

Source: Internet
Author: User

Configparser Module

I. Configparser module

Used to generate and modify common configuration documents, but the Money module name was changed to Configparser in the python3.x version.

1. Generate a configuration.

ImportConfigparserconfig=Configparser. Configparser () config["DEFAULT"] = {'Serveraliveinterval':' $','Compression':'Yes','CompressionLevel':'9'}config['bitbucket.org'] ={}config['bitbucket.org']['User'] ='HG'With Open ('Example.ini','W') as Configfile:config.write (configfile)

Note: Generate Configuration file Example.ini

2. Read the configuration file

Import= configparser. Configparser () Conf.read("example.ini")print( Conf.defaults ())print(conf.sections ())print(conf['  bitbucket.org'['user'])

Note: conf.defaults: Read defaults read in dictionary type

Note: conf.sections: Reads a node and does not contain defaults.

Note: conf[' bitbucket.org '[' user ']: reads the contents of the node directly.

4. Delete the configuration file contents.

Importconfigparserconf=Configparser. Configparser () Conf.read ("Example.ini")Print(Conf.defaults ())Print(Conf.sections ())Print(conf['bitbucket.org']['User']) SEC= Conf.remove_section ('bitbucket.org') conf.write (open ('exmple2.cfg',"W"))

Note: Delete and create a backup within the new file.

Python 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.