本文实例为大家分享了python的ini配置文件操作工具类的具体代码,供大家参考,具体内容如下
#!/usr/bin/env python # -*- coding: utf-8 -*- """ @Time : 2018/6/22 @Author : LiuXueWen @Site : @File : Util_Ini_Operation.py @Software: PyCharm @Description: ini配置文件操作工具类 1.读取.ini配置文件 2.修改.ini配置文件 [section] option:value """ import ConfigParser ''' 基础读取配置文件 -read(filename) 直接读取文件内容 -sections() 得到所有的section,并以列表的形式返回 -options(section) 得到该section的所有option -items(section) 得到该section的所有键值对 -get(section,option) 得到section中option的值,返回为string类型 -getint(section,option) 得到section中option的值,返回为int类型,还有相应的getboolean()和getfloat() 函数。 ''' class get_ini(): # 初始化配置文件对象 def __init__(self,path): # 实例化 self.cf = ConfigParser.ConfigParser() # 读取配置文件 self.cf.read(path) # 获取所有的sections def get_sections(self): sections = self.cf.sections() return sections # 获取section下的所有key def get_options(self,section): opts = self.cf.options(section=section) return opts # 获取section下的所有键值对 def get_kvs(self,section): kvs = self.cf.items(section=section) return kvs # 根据section和option获取指定的value def get_key_value(self,section,option): opt_val = self.cf.get(section=section,option=option) return opt_val # 更新指定section的option下的value # def update_section_option_val(self,section,option,value,path,module): # self.cf.set(section=section,option=option,value=value) # with open(path,module) as f: # self.cf.write(f) ''' 基础写入配置文件 -write(fp) 将config对象写入至某个 .init 格式的文件 Write an .ini-format representation of the configuration state. -add_section(section) 添加一个新的section -set(section, option, value) 对section中的option进行设置,需要调用write将内容写入配置文件 ConfigParser2 -remove_section(section) 删除某个 section -remove_option(section, option) 删除某个 section 下的 option ''' class write_ini(): def __init__(self,path,module): # 实例化配置对象 self.cf = ConfigParser.ConfigParser() # 获取写入文件路径,若采用w+方式则该文件可以不存在 self.path = path # 配置写入方式,写入方式"w+"清空写 self.module = module # 写入配置文件 def write_ini_file(self): with open(self.path,self.module) as f: self.cf.write(f) # 新增section def add_section(self,section): self.cf.add_section(section=section) self.write_ini_file() # 删除某个 section def remove_section(self,section): self.cf.remove_section(section=section) self.write_ini_file() # 删除某个 section 下的 option def remove_option(self,section,option): self.cf.remove_option(section=section,option=option) self.write_ini_file() if __name__ == '__main__': pass
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
标签:
python,ini
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“基于python的ini配置文件操作工具类”评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新动态
2025年05月15日
2025年05月15日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]