时间:2021-07-01 10:21:17 帮助过:36人阅读
写配置一般用ConfigParse.RawConfigParse类
读配置用ConfigParse.ConfigParse类
代码如下:
写配置文件:
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")#增加section 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)#写入文件中
运行结果如下:
[analysis] started = Tue Apr 10 15:40:51 2012 package = exe id = 1 timeout = 150 target = 1.exe
读取配置文件:
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")
打印结果如下:
150 ['analysis'] exe 1
希望本文所述对大家的Python程序设计有所帮助。