Python實(shí)現(xiàn)ini文件操作基本操作方式分享
作者:佚名
我們今天將會在這里為大家詳細(xì)介紹一下有關(guān)Python實(shí)現(xiàn)ini文件操作的應(yīng)用方式,希望大家可以通過我們給出的代碼示例充分掌握這一操作技術(shù)。
Python編程語言對于開發(fā)人員來說是一個(gè)應(yīng)用非常廣泛,功能強(qiáng)大的應(yīng)用語言。其獨(dú)特的功能特點(diǎn)能夠幫助我們輕松的完成各種功能需求。在這里我們一起來看看Python實(shí)現(xiàn)ini文件操作的相關(guān)應(yīng)用方式。
Python實(shí)現(xiàn)ini文件操作代碼示例:
- # -*- 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實(shí)現(xiàn)ini文件操作的相關(guān)應(yīng)用方式就為大家介紹到這里。
【編輯推薦】
責(zé)任編輯:曹凱
來源:
博客園