本文共 839 字,大约阅读时间需要 2 分钟。
configParser 模块用于操作配置文件
配置文件(INI文件)由节(section)、键、值组成1、config=ConfigParser.ConfigParser() 创建ConfigParser实例 2、config.sections() 返回配置文件中节序列 3、config.options(section) 返回某个项目中的所有键的序列 4、config.get(section,option) 返回section节中,option的键值 5、config.add_section(str) 添加一个配置文件节点(str) 6、config.set(section,option,val) 设置section节点中,键名为option的值(val) 7、config.read(filename) 读取配置文件 8、config.write(obj_file) 写入配置文件9、config.get() 返回文本 config.getint()返回整数10、config.remove_option( section, option)11、config.remove_section( section)案例:import ConfigParserimport osclass ReadWriteConfFile:currentDir=os.path.dirname(file) @staticmethodbr/>filepath=currentDir+os.path.sep+"inetMsgConfigure.ini"@staticmethoddef getConfigParser():cf=ConfigParser.ConfigParser()cf.read(ReadWriteConfFile.filepath)return cf转载于:https://blog.51cto.com/weadyweady/2133117