Python实现的ini文件操作类分享

yipeiwu_com5年前Python基础

类代码:

# -*- coding:gbk -*-
import ConfigParser, os
class INIFILE:
  def __init__(self, filename):
    self.filename = filename
    self.initflag = False
    self.cfg = None
    self.readhandle = None
    self.writehandle = None

  def Init(self):
    self.cfg = ConfigParser.ConfigParser()
    try:
      self.readhandle = open(self.filename, 'r')
      self.cfg.readfp(self.readhandle)
      self.writehandle = open(self.filename, 'w')
      self.initflag = True
    except:
      self.initflag = False
    return self.initflag

  def UnInit(self):
    if self.initflag:
      self.readhandle.close()
      self.writehandle.closse()

  def GetValue(self, Section, Key, Default = ""):
    try:
      value = self.cfg.get(Section, Key)
    except:
      value = Default
    return value

  def SetValue(self, Section, Key, Value):
    try:
      self.cfg.set(Section, Key, Value)
    except:
      self.cfg.add_section(Section)
      self.cfg.set(Section, Key, Value)
      self.cfg.write(self.writehandle)

相关文章

Python 找到列表中满足某些条件的元素方法

如下所示: a = [0, 1, 2, 3, 4, 0, 2, 3, 6, 7, 5] selected = [x for x in a if x in range(1, 5)] #...

python 定义类时,实现内部方法的互相调用

每次调用内部的方法时,方法前面加 self. 举例: 例子参考百度知道里面的回答 class MyClass: def __init__(self): pass de...

python3安装speech语音模块的方法

python3安装speech语音模块的方法

在windows平台上使用pyhton编写语音识别程序需要用到speech模块,speech模块支持的主要功能有:文本合成语音,将键盘输入的文本信息转换为语音信号方式输出;语音识别,将输...

简单了解Python下用于监视文件系统的pyinotify包

什么是inotify:   Inotify是一个事件驱动的通知机制,Inotify 提供一个简单的API,使用最小的文件描述符,并且允许细粒度监控。与 inotify 的...

Python 将RGB图像转换为Pytho灰度图像的实例

问题: 我正尝试使用matplotlib读取RGB图像并将其转换为灰度。 在matlab中,我使用这个: img = rgb2gray(imread('image.png'));...