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.