Python实现读写INI配置文件的方法示例
作者:shaomine 时间:2021-03-16 08:10:08
本文实例讲述了Python实现读写INI配置文件的方法。分享给大家供大家参考,具体如下:
# -*- coding: utf-8 -*-
import ConfigParser
import 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程序设计有所帮助。
来源:http://www.cnblogs.com/shaosks/p/6098349.html
标签:Python,配置文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python排序算法之插入排序及其优化方案详解
2021-04-03 05:39:31
![](https://img.aspxhome.com/file/2023/6/72086_0s.png)
Python中22个万用公式的小结
2022-12-15 21:06:38
![](https://img.aspxhome.com/file/2023/6/70906_0s.png)
PHP中round()函数对浮点数进行四舍五入的方法
2023-11-23 21:35:24
Python使用scipy.fft进行大学经典的傅立叶变换
2022-09-10 20:26:13
![](https://img.aspxhome.com/file/2023/3/83863_0s.png)
使用LibTorch进行C++调用pytorch模型方式
2021-09-17 18:56:13
![](https://img.aspxhome.com/file/2023/8/85028_0s.png)
SQL Server 2005返回刚刚插入的数据条目id值
2008-12-04 17:16:00
17个javascript自定义函数库
2008-02-26 11:49:00
序列化Python对象的方法
2022-07-09 22:51:59
Python面向对象三大特征 封装、继承、多态
2022-01-09 04:31:51
![](https://img.aspxhome.com/file/2023/1/92501_0s.png)
python sort、sorted高级排序技巧
2023-02-24 16:20:31
使用Python编写Linux系统守护进程实例
2022-06-09 16:50:52
再谈 MySQL 数据库备份恢复和乱码问题
2009-08-19 09:35:00
php header功能的使用
2023-11-15 09:25:26
python字典的setdefault的巧妙用法
2021-05-18 02:58:48
Silverlight 3 Beta出来了
2009-03-19 13:17:00
在asp中调用sql server的存储过程方法
2007-08-13 13:28:00
![](https://img.aspxhome.com/file/UploadPic/up/2007081313422835.jpg)
简易CSS相册源代码
2008-04-18 12:28:00
使用Title提升可访问性
2009-04-04 17:06:00
![](https://img.aspxhome.com/file/UploadPic/20094/4/title_blueidea-67s.png)
Python使用POP3和SMTP协议收发邮件的示例代码
2023-10-03 15:37:28
![](https://img.aspxhome.com/file/2023/0/62890_0s.jpg)
python pandas.DataFrame选取、修改数据最好用.loc,.iloc,.ix实现
2023-07-25 16:29:47