python读写ini文件示例(python读写文件)
时间:2023-06-21 03:10:14
很类似java的properties文件
xml文件
db_config.ini
[baseconf]
host=127.0.0.1
port=3306
user=root
password=root
db_name=evaluting_sys
[concurrent]
processor=20
对应的python代码
#!/usr/bin/python
# -*- coding:utf-8 -*-
#author: lingyue.wkl
#desc: use to db ops
#---------------------
#2012-02-18 created
#---------------------
import sys,os
import ConfigParser
class Db_Connector:
def __init__(self, config_file_path):
cf = ConfigParser.ConfigParser()
cf.read(config_file_path)
s = cf.sections()
print 'section:', s
o = cf.options("baseconf")
print 'options:', o
v = cf.items("baseconf")
print 'db:', v
db_host = cf.get("baseconf", "host")
db_port = cf.getint("baseconf", "port")
db_user = cf.get("baseconf", "user")
db_pwd = cf.get("baseconf", "password")
print db_host, db_port, db_user, db_pwd
cf.set("baseconf", "db_pass", "123456")
cf.write(open("config_file_path", "w"))
if __name__ == "__main__":
f = Db_Connector("../conf/db_config.ini")
得到结果:
section: ['concurrent', 'baseconf']
options: ['host', 'db_name', 'user', 'password', 'port']
db: [('host', '127.0.0.1'), ('db_name', 'evaluting_sys'), ('user', 'root'), ('password', 'root'), ('port', '3306')]
127.0.0.1 3306 root root
通用模块:支持命令行+import两种形式
ini_op.py
#!/usr/bin/python
# -*- coding:utf-8 -*-
#author: lingyue.wkl
#desc: use to read ini
#---------------------
#2012-02-18 created
#2012-09-02 changed for class support
#---------------------
import sys,os,time
import ConfigParser
class Config:
def __init__(self, path):
self.path = path
self.cf = ConfigParser.ConfigParser()
self.cf.read(self.path)
def get(self, field, key):
result = ""
try:
result = self.cf.get(field, key)
except:
result = ""
return result
def set(self, filed, key, value):
try:
self.cf.set(field, key, value)
cf.write(open(self.path,'w'))
except:
return False
return True
def read_config(config_file_path, field, key):
cf = ConfigParser.ConfigParser()
try:
cf.read(config_file_path)
result = cf.get(field, key)
except:
sys.exit(1)
return result
def write_config(config_file_path, field, key, value):
cf = ConfigParser.ConfigParser()
try:
cf.read(config_file_path)
cf.set(field, key, value)
cf.write(open(config_file_path,'w'))
except:
sys.exit(1)
return True
if __name__ == "__main__":
if len(sys.argv) < 4:
sys.exit(1)
config_file_path = sys.argv[1]
field = sys.argv[2]
key = sys.argv[3]
if len(sys.argv) == 4:
print read_config(config_file_path, field, key)
else:
value = sys.argv[4]
write_config(config_file_path, field, key, value)
第二个示例
import os
import ConfigParser
def main():
cp = ConfigParser.ConfigParser()
cf = open(u"in.ini")
cp.readfp(cf)
secs = cp.sections()
print cp.sections()
for sec in secs:
opts = cp.options(sec)
for opt in opts:
val = cp.get(sec, opt)
val += "test....."
cp.set(sec, opt, val)
cp.write(open("out.ini", "w"))
if __name__ == '__main__':
main()
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
go语言发送smtp邮件的实现示例
![](https://img.aspxhome.com/file/2023/7/72727_0s.png)
对Pycharm创建py文件时自定义头部模板的方法详解
神经网络训练采用gpu设置的方式
![](https://img.aspxhome.com/file/2023/8/135118_0s.jpg)
Oracle中的分析函数汇总
![](https://img.aspxhome.com/file/2023/1/67031_0s.jpg)
用Python判断奇偶数示例
![](https://img.aspxhome.com/file/2023/5/97545_0s.png)
Python中socket网络通信是干嘛的
Bottle部署web服务及postman接口的方法
![](https://img.aspxhome.com/file/2023/7/112057_0s.png)
详解golang defer 闭包 匿名函数
关于ASP生成伪参数加密技巧
一步步教你编写可测试的Go语言代码
Python requests发送post请求的一些疑点
![](https://img.aspxhome.com/file/2023/9/77609_0s.png)
在ASP.NET 2.0中操作数据之一:创建一个数据访问层
![](https://img.aspxhome.com/file/2023/6/75526_0s.png)
vue中iframe的使用及说明
Python采集大学教务系统成绩单实战示例
![](https://img.aspxhome.com/file/2023/5/72715_0s.jpg)
python 函数传参之传值还是传引用的分析
![](https://img.aspxhome.com/file/2023/2/123802_0s.jpg)
使用PHP实现微信摇一摇周边红包
![](https://img.aspxhome.com/file/2023/9/78289_0s.jpg)
python数据处理 根据颜色对图片进行分类的方法
![](https://img.aspxhome.com/file/2023/5/103765_0s.jpg)
如何修改Linux的下MySQL 5.0的默认连接数
Django接收post前端返回的json格式数据代码实现
JavaWeb实现显示mysql数据库数据
![](https://img.aspxhome.com/file/2023/7/67187_0s.png)