国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python實現讀寫INI配置文件的方法示例

2020-02-15 21:45:35
字體:
來源:轉載
供稿:網友

本文實例講述了Python實現讀寫INI配置文件的方法。分享給大家供大家參考,具體如下:

# -*- coding: utf-8 -*-import ConfigParserimport os'''讀寫配置文件的類[section]logpath = D:/log/imageminsize = 200'''class ConfigFile:  '''構造函數:初始化'''  def __init__(self,fileName):    fileName = unicode(fileName,'utf8')    self.flag = False    if os.path.isfile(fileName):      self.fileName = fileName      self.cf = ConfigParser.ConfigParser()      self.cf.read(self.fileName)      self.flag = True  '''獲取節為section,鍵值為Key的值'''  def GetValue(self,Section, Key):    if self.flag:      try:        result = self.cf.get(Section, Key)        return result      except Exception,e:        print e        return ""    else:      return ""  def SetValue(self,Section, Key,Value):    if self.flag:      try:        self.cf.set(Section, Key, Value)        self.cf.write(open(self.fileName, "w"))      except Exception,e:        print e        return ""#測試代碼configfile = os.path.join(os.getcwd(),'config.conf')cf = ConfigFile(configfile)print cf.GetValue("section","logpath")cf.SetValue("section","imageminsize","200")

更多關于Python相關內容感興趣的讀者可查看本站專題:《Python函數使用技巧總結》、《Python面向對象程序設計入門與進階教程》、《Python數據結構與算法教程》、《Python字符串操作技巧匯總》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總》

希望本文所述對大家Python程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 清苑县| 三台县| 靖宇县| 鄂尔多斯市| 灌南县| 寻甸| 澄城县| 兰州市| 潞西市| 东明县| 读书| 莎车县| 界首市| 启东市| 龙陵县| 凉山| 福州市| 金寨县| 承德县| 庄河市| 阜南县| 五原县| 福海县| 宜川县| 通化市| 隆德县| 巫溪县| 红桥区| 衡东县| 临澧县| 那曲县| 台东县| 永安市| 青岛市| 扶沟县| 庆元县| 石家庄市| 建水县| 科技| 盘锦市| 汉寿县|