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调用c++返回带成员指针的类指针实例

这个是OK的: class Rtmp_tool { public: int m_width; AVCodecContext * c; }; 指针的用法如下: Rtm...

关于Python中异常(Exception)的汇总

前言 Exception类是常用的异常类,该类包括StandardError,StopIteration, GeneratorExit, Warning等异常类。python中的异常使用...

python使用writerows写csv文件产生多余空行的处理方法

初次接触python,学艺不精,第一次实战写一个文本处理的小程序时便遇到了头疼的问题。 先看代码: 生成的.CSV文件每两行之间都会多出一行空格(如下图),具体原因可参看点击打开链接...

Python实现压缩和解压缩ZIP文件的方法分析

本文实例讲述了Python实现压缩和解压缩ZIP文件的方法。分享给大家供大家参考,具体如下: 有时我们需要在 Python 中使用 zip 文件,而在1.6版中,Python 就已经提供...

python看某个模块的版本方法

例如networkx模块 启动python命令行,输入以下两行命令 import networkx networkx.__version__ 以上这篇python看某个模块的版本方...