ConfigParse module usage in python

Source: Internet
Author: User
This article describes how to use the ConfigParse module in python and describes how to use the ConfigParse module in an instance, for more information about how to use the ConfigParse module in python, see the following example. The specific method is as follows:

The ConfigParse. RawConfigParse class is generally used for write configuration.

Use ConfigParse. ConfigParse class for read configuration

The code is as follows:

Write the configuration file:

Import ConfigParser import time config = ConfigParser. rawConfigParser () task ={} task ["id"] = 1 task ["package"] = "exe" task ["timeout"] = 150 task ["dst_filename"] = "1.exe" task ["custom"] = "" config. add_section ("analysis") # Add section config. set ("analysis", "id", task ["id"]) # Add option config. set ("analysis", "target", task ["dst_filename"]) config. set ("analysis", "package", task ["package"]) config. set ("analysis", "timeout", task ["timeout"]) config. set ("analysis", "started", time. asctime () fp = open ("analy. conf "," w ") config. write (fp) # Writing to a file

The running result is as follows:

[analysis]started = Tue Apr 10 15:40:51 2012package = exeid = 1timeout = 150target = 1.exe

Read the configuration file:

import ConfigParser  config = ConfigParser.ConfigParser()  config.read("analy.conf") if config.has_option("analysis", "timeout"):   print config.get("analysis", "timeout")    print config.sections() print config.get("analysis", "package") print config.getint("analysis", "id") 

The output is as follows:

150['analysis']exe1

I hope this article will help you with Python programming.

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.