Python Basic---Function Module 4 (configparser module)

Source: Internet
Author: User

Configparser Module

One,configparser module

1, what is configparser module :configparser module operation configuration file, configuration file format similar to Windows INI and Linux CF file, can contain one or more sections (section) , each section can have multiple parameters (key = value), and its configuration file (INI file) consists of a section, a key, and a value.

2,configparser module Introduction.

Configparser is the package that is used to read the configuration file. The format of the configuration file is as follows: the brackets "[]" are included in the section. section below is a configuration content similar to Key-value.

3, how to generate a configparser configuration file.

1. First import Configparser to call configparser Module #import configparser

2. Create a Configparser configuration file object. #config = configparser. Configparser ()

3. Add content to the configuration file. #config["DEFAULT"] = {' Serveraliveinterval ': ' n ', ' Compression ': ' Yes '}; config[' bitbucket.org '] = {' User ': ' H G '}

4. Open or create a profile, and then add something inside the profile object to the configuration file.

      With open (' Example.ini ', ' W ') as ConfigFile:

        Config.write (ConfigFile)

5. Read the configuration file. #config.read (' config file name ')

6. When parsing a configuration file using the Configparser module, the problem is found:

The capitalization of the parameter names is all converted to lowercase.

Parameter names cannot contain [,]

If more than one section with the same name is included, the last section will prevail

7. Read the configuration file and write the configuration file.

-read (filename) reads INI file contents directly
-sections () gets all the sections and returns as a list
-options (section) gets all the option of the section
-items (section) gets all the key-value pairs of the section
-get (section,option) Gets the value of option in section, returned as a string type
-getint (section,option) Gets the value of option in section and returns the int type

-add_section (section) Add a new section
-set (section, option, value) to set the option in sections

8. Case Test

See: http://blog.csdn.net/gexiaobaohelloworld/article/details/7976944

Second, subprocess module

1.

   

Python Basic---function Module 4 (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.