Python读取ini配置文件

简介: db_config.ini [baseconf] host=127.0.0.1 port=3306 user=root password=root db_name=evaluting_sys [concurrent] processor=20 ...
db_config.ini  
[baseconf]  
host=127.0.0.1  
port=3306  
user=root  
password=root  
db_name=evaluting_sys  
[concurrent]  
processor=20  

python代码

 1 对应的python代码
 2 import sys,os  
 3 import ConfigParser  
 4   
 5 class Db_Connector:  
 6   def __init__(self, config_file_path):  
 7     cf = ConfigParser.ConfigParser()  
 8     cf.read(config_file_path)  
 9   
10     s = cf.sections()  
11     print 'section:', s  
12   
13     o = cf.options("baseconf")  
14     print 'options:', o  
15   
16     v = cf.items("baseconf")  
17     print 'db:', v  
18   
19     db_host = cf.get("baseconf", "host")  
20     db_port = cf.getint("baseconf", "port")  
21     db_user = cf.get("baseconf", "user")  
22     db_pwd = cf.get("baseconf", "password")  
23   
24     print db_host, db_port, db_user, db_pwd  
25   
26     cf.set("baseconf", "db_pass", "123456")  
27     cf.write(open("config_file_path", "w"))  
28 if __name__ == "__main__":  
29   f = Db_Connector("../conf/db_config.ini")  

通用模块:支持命令行+import两种形式

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)  

 

技术改变世界! --狂诗绝剑
目录
相关文章
|
7月前
|
Python
百度搜索:蓝易云【Python 使用ConfigParser操作ini配置文件教程。】
请注意,实际的使用可能涉及更复杂的配置文件结构和操作。你可以参考 `ConfigParser`的官方文档以获取更多详细信息和示例。
238 0
|
1月前
|
存储 XML JSON
Python如何读写配置文件?
Python如何读写配置文件?
24 0
|
3月前
|
存储 BI 网络安全
正在等待继续编辑 - Python - 基础知识专题 - 配置文件与日志管理
正在等待继续编辑 - Python - 基础知识专题 - 配置文件与日志管理
22 0
|
3月前
|
编解码 IDE 开发工具
python ini文件包含中文时报错UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0x8c 的解决办法
python ini文件包含中文时报错UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0x8c 的解决办法
41 1
|
8月前
|
JSON 关系型数据库 MySQL
Python--toml配置文件
Python--toml配置文件
121 0
|
4月前
|
Python
python对 ini 文件的读取
python对 ini 文件的读取
|
7月前
|
关系型数据库 MySQL 测试技术
3分钟学会Python 常用配置文件处理
### 1\. 什么是配置文件 配置文件是为程序配置参数和初始设置的文件。一般为文本文件,以`ini`,`conf`,`cnf`,`cfg`,`yaml`等作为后缀名。
|
8月前
|
数据格式
Python--配置文件优化
Python--配置文件优化
30 0
|
8月前
|
关系型数据库 MySQL 数据处理
Python--Yaml配置文件
Python--Yaml配置文件
69 0
|
8月前
|
JSON 关系型数据库 MySQL
Python--json配置文件
Python--json配置文件
83 0