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.M. Kuchling (amk@amk.ca) 授权许可:创作共享协议 翻译人员:FireHare 校对人员:Leal 适用版本:Python 1.5 及后续版本http...

pygame实现贪吃蛇游戏(下)

pygame实现贪吃蛇游戏(下)

接着上篇pygame实现贪吃蛇游戏(上)继续介绍 1.豆子的吃掉效果 只需在代码最后移动蛇头的代码后增加一个蛇头和豆子坐标的判断即可 if snake_x == bean_x and...

PyQt5创建一个新窗口的实例

PyQt5创建一个新窗口的实例

1.使用Qt Design设计一个空白窗口(注意:我是使用MainWindow创建的窗口) 2.使用PyUIC将.ui文件转换成.py文件 右键.ui文件->External T...

python查看列的唯一值方法

查看某一列中有多少中取值: 数据集名.drop_duplicates(['列名']) #实际为删除重复项,删除后对原数据集不修改 输入:data.drop_duplicates(['na...

python字符串的拼接方法总结

python字符串的拼接方法总结

这篇文章主要介绍了python字符串的拼接方法总结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 加号连接 1.通过+号连接起来 逗...