The use of Configparse modules in Python _python

Source: Internet
Author: User

This article illustrates the use of Configparse modules in Python and shares them for your reference. The specific methods are as follows:

Write Configuration general Use Configparse.rawconfigparse class

Read configuration with Configparse.configparse class

The code is as follows:

Write configuration file:

Import configparser 
import time  
config = configparser.rawconfigparser () 
 
task = {} 
task["id" = 1 
Task["package"] = "EXE" 
task["Timeout"] = 
task["dst_filename"] = "1.exe" 
task["custom"] = "" 
Config.add_section ("Analysis") #增加section the Config.set ("Analysis 
", "id", task["id"]) #增加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) #写入文件中 

The results of the operation are as follows:

[Analysis]
started = Tue APR 15:40:51
package = exe
ID = 1
timeout =
target = 1.exe

To read a 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") 

Print the results as follows:


[' analysis ']
exe
1

I hope this article will help you with your 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.