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. PHP绿色集成环境在云服务器上的应用,PHPWAMP在服务器上搭建网站案例

    问:什么叫WAMP?答:Windows下的Apache+Mysql+PHP,称之为WAMP. 本文案例采用的PHP集成环境是我自己开发的纯绿色版WAMP软件(PHPWAMP). 我在这款集成环境里集成 ...

  2. 51nod1279(二分)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1279 题意:中文题诶- 思路: 就想短板效应一样,很显然决定 ...

  3. 有关linux标准输出、标准输入、标准错误的重定向问题

    1.简单的命令行重定向问题. 例:ls  -al  test   test1  test2    1>out.txt  2>err.tx 这里ls这句命令行命令之后将标准输入重定向到out ...

  4. iOS 手势识别

    首先给大家解释一下为什么要学习手势识别? 如果想监听一个UIView上面的触摸事件,之前的做法是: 自定义一个UIView : 实现UIView的touches方法,在方法里面实现具体功能 透过tou ...

  5. python 数据清洗之字符串处理

    在数据分析中,特别是文本分析中,字符处理需要耗费极大的精力, 因而了解字符处理对于数据分析而言,也是一项很重要的能力. 字符串处理方法 首先我们先了解下都有哪些基础方法 首先我们了解下字符串的拆分sp ...

  6. Windows下载地址

    文件名 cn_windows_7_professional_with_sp1_x64_dvd_u_677031.iso SHA1 9B57E67888434C24DD683968A3CE2C72755 ...

  7. nyoj 79 拦截导弹 (动态规划)

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=79 题意即求最长单调递减子序列 #include<iostream> #inc ...

  8. 实现自己的JDBC框架

    使用JDBC操作数据库时,dao层的增删改查有很多重复的代码,比如下面的 public int getTotal() { Connection conn = null;//通用代码 PreparedS ...

  9. string,stringbuilder和stringbuffer的区别

    String.StringBuffer.StringBuilder之间的区别(网上收集) StringBuffer.StringBuilder和String一样,也用来代表字符串.String类是不可 ...

  10. android下拉刷新控件 android-pulltorefresh

    运行效果: 介绍:ListView.ViewPager.WevView.ExpandableListView.GridView.(Horizontal)ScrollView.Fragment上下左右拉 ...