Python读取修改ini配置文件[ConfigParser]
来源:互联网 发布:php字符型转换函数 编辑:程序博客网 时间:2024/05/29 07:17
使用到的包:ConfigParser 文档
代码托管位置 github-pytools
需求
写个项目,用到数据库,多个地方使用,不能硬编码。很类似java的properties文件
Python支持ini文件的读取
ini文件
db_config.ini
1
2
3
4
5
6
7
8
[baseconf]
host=127.0.0.1
port=3306
user=root
password=root
db_name=evaluting_sys
[concurrent]
processor=20
示例代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/python
# -*- coding:utf-8 -*-
#author: lingyue.wkl
#desc: use to db ops
#---------------------
#2012-02-18 created
#---------------------
importsys,os
importConfigParser
deftest(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")
printdb_host,db_port,db_user,db_pwd
cf.set("baseconf","db_pass","123456")
cf.write(open("config_file_path","w"))
if__name__=="__main__":
test("../conf/db_config.ini")
通用模块init_op.py
支持命令行+import两种
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/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
#---------------------
importsys,os,time
importConfigParser
classConfig:
def__init__(self,path):
self.path=path
self.cf=ConfigParser.ConfigParser()
self.cf.read(self.path)
defget(self,field,key):
result=""
try:
result=self.cf.get(field,key)
except:
result=""
returnresult
defset(self,filed,key,value):
try:
self.cf.set(field,key,value)
cf.write(open(self.path,'w'))
except:
returnFalse
returnTrue
defread_config(config_file_path,field,key):
cf=ConfigParser.ConfigParser()
try:
cf.read(config_file_path)
result=cf.get(field,key)
except:
sys.exit(1)
returnresult
defwrite_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)
returnTrue
if__name__=="__main__":
iflen(sys.argv) 4:
sys.exit(1)
config_file_path=sys.argv[1]
field=sys.argv[2]
key=sys.argv[3]
iflen(sys.argv)==4:
printread_config(config_file_path,field,key)
else:
value=sys.argv[4]
write_config(config_file_path,field,key,value)
阅读全文
0 0
- Python读取修改ini配置文件[ConfigParser]
- python读取配置文件configparser
- Python configparser修改配置文件
- Python利用ConfigParser读取配置文件
- Python 之 ConfigParser : 配置文件读取
- Python利用ConfigParser读取配置文件
- python之ConfigParser读取配置文件
- Python用ConfigParser解析INI配置文件
- python 3.x 解析 INI配置文件(ConfigParser)
- Python读取ini配置文件
- Python读取ini配置文件
- Python读取INI配置文件
- Python 读取写入配置文件 —— ConfigParser
- Python读取配置文件:ConfigParser.NoSectionError: No section:
- python 中使用ConfigParser类修改配置文件
- python 使用ConfigParser解析&修改配置文件
- 通过python读取ini配置文件
- ConfigParser 读取 config 配置文件
- 【LibUIDK系列文章】VC的内存泄漏检查
- 三、JS【JavaScript弱类型的脚本语言之BOM】05
- java基础【3】
- 第一篇博客
- 有机会
- Python读取修改ini配置文件[ConfigParser]
- 题目488:素数环
- javaEE MVC模式 提交订单和支付功能
- 马踏棋盘
- win10 代理服务器出现问题 或者地址有误
- 自定义类型的嵌套定义
- 面试题
- Bootstrap学习
- 027 反三角函数的求导公式推导