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

python代码

 对应的python代码
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", "")
cf.write(open("config_file_path", "w"))
if __name__ == "__main__":
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()
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()
return True if __name__ == "__main__":
if len(sys.argv) < :
sys.exit() config_file_path = sys.argv[]
field = sys.argv[]
key = sys.argv[]
if len(sys.argv) == :
print read_config(config_file_path, field, key)
else:
value = sys.argv[]
write_config(config_file_path, field, key, value)

Python读取ini配置文件的更多相关文章

  1. python读取ini配置文件的示例代码(仅供参考)

    这篇文章主要介绍了python读取ini配置文件过程示范,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 安装 pip install configp ...

  2. Python读取ini配置文件(接口自动测试必备)

    前言 大家应该接触过.ini格式的配置文件.配置文件就是把一些配置相关信息提取出去来进行单独管理,如果以后有变动只需改配置文件,无需修改代码. 特别是后续做自动化的测试,代码和数据分享,进行管理.比如 ...

  3. python读取 ini 配置文件

    在详解python读取ini文件之前,我们先说明一个ini文件的组成: 一个ini文件是由多个section组成,每个section中以key=vlaue形式存储数据: 然后我们来使用python读取 ...

  4. Python读取ini配置文件的方式

    python configparser模块   ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...

  5. Python读取ini配置文件封装方法

    读取配置文件 ----rw_ini.py from configparser import ConfigParser def read_config(config_file_path:str): &q ...

  6. python 读取ini 配置文件

    安装 pip install configparser 1 配置文件 config.ini: [MysqlDB]user=rootpasswd=123456sport=3306db_name=my_d ...

  7. Python 读取写入配置文件 —— ConfigParser

    Python 读取写入配置文件 —— ConfigParser Python 读取写入配置文件很方便,可使用内置的 configparser 模块:可查看源码,如博主本机地址: “C:/python2 ...

  8. 转 python3 读取 ini配置文件

    在代码中经常会通过ini文件来配置一些常修改的配置.下面通过一个实例来看下如何写入.读取ini配置文件. 需要的配置文件是: 1 [path] 2 back_dir = /Users/abc/Pych ...

  9. python读取ini文件

    import configparser import os config=configparser.ConfigParser()#创建config对象 file_path=os.path.dirnam ...

随机推荐

  1. GMap学习笔记

    GMap学习笔记 1.GMap体系详解 What is the map control (GMapControl)? This is the control which renders the map ...

  2. template template parameter

    #include <iostream> using namespace std; template<typename T> class A { }; template<t ...

  3. Form 提交表单

    ---HTML <form action="/Sample/SampleList/ExportSample" style="display:inline" ...

  4. DOG角点检测——opencv实现

    1.原理 Difference of Gaussian(DOG)是高斯函数的差分.将两幅图像在不同参数下的高斯滤波结果相减,得到DoG图.步骤: 处理一幅图像在不同高斯参数下的DoG 用两个不同的5x ...

  5. radio里面value值与其他字符进行比较

    本题旨在创建一个具有及时反馈的选择题,当选完其中一个选项后,会有弹窗来提醒你选择的答案是正确还是错误的.<div id="text" style="display: ...

  6. perl的列表(List)和数组(Array)

    If a scalar is the "singular" in Perl, as we described it at the beginning of Chapter 2, t ...

  7. wpf 寻找某个控件下的子控件

    /// <summary> /// 寻找某个控件下的子控件 /// </summary> /// <typeparam name="ChildType" ...

  8. Elasticsearch 5.0 _all field的简单认识

    前言:本文的目的是为后续磁盘空间利用优化做铺垫,主要知识点来源于官网 一._all 是什么 在Elasticsearch中,_all field维护这一个很大的字符串数组(text类型).这个字符串是 ...

  9. Android使用Home键后应用程序重启的问题

    正常情况应该是在点击 home 按键后,程序在后台暂停运行,点击 后退键 才会退出应用的,但是今天遇到个问题,点击 home 键后,重新再打开应用却每次都返回应用启动页面,有些莫名其妙,一番googl ...

  10. Android设置窗体Activity背景透明

    背景透明 style.xml <item name="android:windowBackground">@color/transparent</item> ...