Python的配置文件


配置文件

setting.ini文件是一个纯文本
[DataBase1]
username = admin
passwors = root [DataBase2]
hostname = 127.0.0.1
port = 3306

这是一般配置文件的常见的格式,[]里面叫做Section部分的名称,整个[]以及下面的每一项(每一个key=value,叫做Option)都是一个Section区域

Python读取配置文件

#引入依赖库,Python2.x引入ConfigParser Python3.x引入configparser
try:
import ConfigParser as cReader
except Exception as reason:
import configparser as cReader

要实现的功能

# -*- coding:utf-8 -*-
"""
自定义配置文件读取库
调用ConfigParser,只实现常用功能,作为学习ConfigParser和自用库
""" #引入依赖库,Python2.x引入ConfigParser Python3.x引入configparser
import json
try:
import ConfigParser as cReader
except Exception as reason:
import configparser as cReader #定义类和函数
class ConfigChannel:
"""建立配置读取类""" def __init__(self, configFile):
"""建立ConfigChannel对象"""
try:
self.configFile = configFile
self.channel = cReader.SafeConfigParser()
self.channel.read(open(configFile))
except Exception as reason:
raise def getOption(self, Section, Option):
"""获取某区域的某一项配置"""
try:
return self.channel.getfloot(Section, Option)
except Exception as reason:
try:
return self.channel.getint(Section, Option)
except Exception as reason:
try:
return self.channel.getboolean(Section, Option)
except Exception as reason:
return self.channel.get(Section, Option) def hasOption(self, Section, Option):
"""判断是否存在这个Option"""
try:
return self.channel.has_option(Section, Option)
except Exception as reason:
raise def showOptions(self):
"""展示所有Options"""
try:
return self.channel.options()
except Exception as reason:
raise def setOption(self, Section, Option, Value):
"""会写或增加配置某区域某一项配置"""
try:
self.channel.set(Section, Option, Value)
self.channel.write(open(self.configFile, "w"))
except Exception as reason:
raise def rmvOption(self, Section, Option):
"""删除某一个配置项"""
try:
self.channel.remove_option(Section, Option)
self.channel.write(open(self.configFile, "w"))
except Exception as reason:
raise def hasSection(self, Section):
"""判断是否存在这个Section"""
try:
return self.channel.has_section(Section)
except Exception as reason:
raise def showOptions(self):
"""展示所有Sections"""
try:
return self.channel.sections()
except Exception as reason:
raise def addSection(self, Section):
"""增加一个配置区域"""
try:
self.channel.add_section(Section)
self.channel.write(open(self.configFile, "w"))
except Exception as reason:
raise def rmvSection(self, Section):
"""删除一个配置区域"""
try:
self.channel.remove_section(Section)
self.channel.write(open(self.configFile, "w"))
except Exception as reason:
raise def getConfigFromString(self, string):
"""从字符串读取配置并写入"""
try:
_dictionary = json.loads(string)
return self.getConfigFromDictionary(_dictionary)
except Exception as reason:
pass
try:
configList = string.split(":")
if len(configList) != 3 or '' in configList:
raise Exception
Section = configList[0]
Option = configList[1]
Value = configList[2]
if self.hasSection(Section):
self.setOption(Section, Option, Value)
else:
self.addSection(Section)
self.setOption(Section, Option, Value)
except Exception as reason:
raise def getConfigFromDictionary(self, dictionary):
"""从字典读取配置并写入"""
if not isinstance(dictionary, dict):
raise
try:
for key in dictionary:
if not isinstance(dictionary.get(key), dict):
continue
if not self.channel.hasSection(key):
self.addSection(key)
for subkey in dictionary[key]:
self.channel.setOption(key, subkey,
dictionary[key][subkey])
except Exception as reason:
raise

熟悉使用ConfigParser库读写配置文件的更多相关文章

  1. Python自动化测试 -ConfigParser模块读写配置文件

    C#之所以容易让人感兴趣,是因为安装完Visual Studio, 就可以很简单的直接写程序了,不需要做如何配置. 对新手来说,这是非常好的“初体验”, 会激发初学者的自信和兴趣. 而有些语言的开发环 ...

  2. python:实例化configparser模块读写配置文件

    之前的博客介绍过利用python的configparser模块读写配置文件的基础用法,这篇博客,介绍下如何实例化,方便作为公共类调用. 实例化的好处有很多,既方便调用,又降低了脚本的维护成本,而且提高 ...

  3. python:利用configparser模块读写配置文件

    在自动化测试过程中,为了提高脚本的可读性和降低维护成本,将一些通用信息写入配置文件,将重复使用的方法写成公共模块进行封装,使用时候直接调用即可. 这篇博客,介绍下python中利用configpars ...

  4. 用ConfigParser模块读写配置文件——Python

    对于功能较多.考虑用户体验的程序,配置功能是必不可少的,如何存储程序的各种配置? 1)可以用全局变量,不过全局变量具有易失性,程序崩溃或者关闭之后配置就没了,再者配置太多,将变量分配到哪里也是需要考虑 ...

  5. Python自动化测试 (二) ConfigParser模块读写配置文件

    ConfigParser 是Python自带的模块, 用来读写配置文件, 用法及其简单. 直接上代码,不解释,不多说. 配置文件的格式是: []包含的叫section,    section 下有op ...

  6. configparser模块读写ini配置文件

    在自动化测试过程中,为了提高脚本的可读性和降低维护成本,将一些通用信息写入配置文件,将重复使用的方法写成公共模块进行封装,使用时候直接调用即可. 这篇博客,介绍下python中利用configpars ...

  7. ConfigParser 读写配置文件

    一.ini: 1..ini 文件是Initialization File的缩写,即初始化文件,是windows的系统配置文件所采用的存储格式 2.ini文件创建方法: (1)先建立一个记事本文件.(2 ...

  8. Python-通过configparser读写配置文件

    Python读写配置文件: 1.创建配置文件(文件名以.conf或.ini结束的文件表示配置文件) 2.导入所需模块 OS, configparser >>> import os & ...

  9. python利用ConfigParser读写配置文件

    ConfigParser 是Python自带的模块, 用来读写配置文件, 用法非常简单. 配置文件的格式是: []包含的叫section,    section 下有option=value这样的键值 ...

随机推荐

  1. Android 获取内存信息

    由于工作需要,研究了一下android上获取内存信息的方法,总结如下: 1.SDK获取 在Java层利用API获取很简单,直接使用ActivityManager.MemoryInfo类即可,代码如下: ...

  2. Java编程思想学习笔记——访问权限修饰词

    几种访问权限修饰词 public,protected,private,friendly(Java中并无该修饰词,即包访问权限,不提供任何访问修饰词) 使用时,放置在类中成员(域或方法)的定义之前的,仅 ...

  3. Android 混淆代码有关问题总结

    Android 混淆代码问题总结 Android 混淆代码: 最快的方式: 1. 首先更新Android的SDK至最新版本,重新建立1个工程,把源码和资源及其他文件拷到新的工程里面. 2. 工程目录底 ...

  4. 怎么解决BarTender因为未检测到IIS安装失败的问题

    个别小伙伴在安装BarTender条码标签设计软件的时候,遇到“未检测到IIS,无法安装BarTender Web Print Server配套程序”导致安装失败的问题,本文小编给大家分享解决BarT ...

  5. windows下WAMP php5.x redis扩展

    其解压到php的扩展目录ext下,在php.ini文件中扩展部分增加一行:extension=php_redis.dll 新增下载地下: php5.3 http://download.csdn.net ...

  6. Mongodb安全认证

    Mongodb安全认证在单实例和副本集两种情况下不太一样,单实例相对简单,只要在启动时加上 --auth参数即可,但副本集则需要keyfile. 一.单实例 1.启动服务(先不要加auth参数) 2. ...

  7. Vim 操作符命令和动作命令

    http://www.cnblogs.com/littlerabbit/articles/2349951.html

  8. Activiti 5.1.4最佳实践

    1.简单介绍 Activiti是一个开源的工作流引擎,它实现了BPMN 2.0规范,可以发布设计好的流程定义,并通过api进行流程调度. Activiti 作为一个遵从 Apache 许可的工作流和业 ...

  9. 【Windows socket+IP+UDP+TCP】网络基础

    Windows Socket+网络      Winsock是 Windows下套接字标准.          Winsock 编程分为UDP[Windows socket + UDP],TCP[Wi ...

  10. 在SSH框架中,如何得到POST请求的URL和参数列表

    在做项目的API通知接口的时候,发现在SSH框架中无法获取到对方服务器发来的异步通知信息.最后排查到的原因可能是struts2对HttpServletRequest进行了二次处理,那么该如何拿到pos ...