# -*- coding: utf-8 -*-
'''
Version : Python27
Author : Spring God
Date : 2012-4-26
Info : 配置文件ini所在文件夹必需存在,否则抛出IOError异常,自处理之
''' import sys if sys.hexversion >= 0x03000000:
import configparser
else:
import ConfigParser as configparser def set_conf(ini_file, section, key, value): _config = configparser.ConfigParser() try:
_config.read(ini_file)
if not _config.has_section(section):
_config.add_section(str(section))
_config.set(str(section), str(key), str(value))
with open(ini_file, 'w') as _file:
_config.write(_file)
_file.close()
del _file
finally:
del _config def get_conf(ini_file, section, key): _config = configparser.ConfigParser() try:
_config.read(ini_file)
value = _config.get(str(section), str(key))
except configparser.NoSectionError:
value = None
finally:
del _config return value def del_conf(ini_file, section, key = None): _config = configparser.ConfigParser() try:
_config.read(ini_file)
if section != None and _config.has_section(section):
if key != None:
_config.remove_option(section, key)
else:
_config.remove_section(section)
with open(ini_file, 'w') as _file:
_config.write(_file)
_file.close()
del _file
finally:
del _config if __name__ == '__main__': set_conf('set.ini', 'General', 'key', 'value')
#print(get_conf('set.ini', 'General', 'key'))
del_conf('set.ini', 'General', 'key')

python配置文件操作——configparser模块的更多相关文章

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

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

  2. Python 标准库 ConfigParser 模块 的使用

    Python 标准库 ConfigParser 模块 的使用 demo #!/usr/bin/env python # coding=utf-8 import ConfigParser import ...

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

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

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

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

  5. Python语言的configparser模块便捷的读取配置文件内容

    配置文件是在写脚本过程中经常会用到的,所以读取配置文件的模块configparser也非常重要,但是很简单. 首先我们的配置文件内容为: 这样的配置文件,[]里面的内容叫section,[]下面的内容 ...

  6. python学习之 - configparser模块

    configparser模块功能:用于生成和修改常见配置文件.基本常用方法如下: read(filename):直接读取配置文件write(filename):将修改后的配置文件写入文件中.defau ...

  7. Python读写操作Excel模块_xlrd_xlwt_xlutils

    Python 读写操作Excel -- 安装第三方库(xlrd.xlwt.xlutils.openpyxl) 如果仅仅是要以表单形式保存数据,可以借助 CSV 格式(一种以逗号分隔的表格数据格式)进行 ...

  8. python中的ConfigParser模块

    1.简介 我们经常需要使用配置文件,例如.conf和.ini等类型,使用ConfigPaser模块可以对配置文件进行操作. 2.示例 现有配置文件test.ini,其内容如下: [section_a] ...

  9. python学习 day19 configparser模块 os模块 subprocess模块

    上周五回顾 logging 用于记录日志 四种核心角色: 生成器Logger 过滤器Filter 处理器Handler 格式化处理器 Formatter logging.info.debug 使用默认 ...

随机推荐

  1. 【bzoj题解】2186 莎拉公主的困惑

    题目传送门. 题意:求\([1,n!]\)中与\(m!\)互质的数的个数,对质数\(R\)取模,\(n\geq m\). 答案应该等于\(\frac{n!}{m!}\phi(m!)=\frac{n!} ...

  2. 如何基于Spring Boot搭建一个完整的项目

    前言 使用Spring Boot做后台项目开发也快半年了,由于之前有过基于Spring开发的项目经验,相比之下觉得Spring Boot就是天堂,开箱即用来形容是绝不为过的.在没有接触Spring B ...

  3. django Rest Framework----GenericAPIView 通用视图 GenericAPIView源码分析

    一.GenericAPIView GenericAPIView扩展了APIView,为标准列表和详细视图添加了常见的行为. 提供的每个具体通用视图都是一个GenericAPIView或多个mixin类 ...

  4. OSGiBundle出现 Could not find bundle: org.eclipse.equinox.console的解决方案

    按照网上教程创建OSGI HelloWorld实例配置run configuration时出现Could not find bundle: org.eclipse.equinox.console 和C ...

  5. shell中的特殊符号总结

    在shell中常用的特殊符号罗列如下: # ;   ;; . , / \\ 'string'| !   $   ${}   $? $$   $* \"string\"* **   ...

  6. HOG目标检测

    用HOG进行行人检测时,需要用训练好的支持向量机来对图片进行分类,在opencv中,支持向量机已经训练好,但自己来训练支持向量机才能更好的体会这一过程. 参考:http://blog.csdn.net ...

  7. 使用html+css+js实现日历与定时器,看看今天的日期和今天剩余的时间。

    使用html+css+js实现日历与定时器,看看今天的日期和今天剩余的时间. 效果图: 哎,今天就又这么过去了,过的可真快 . 代码如下,复制即可使用: <!DOCTYPE html> & ...

  8. H5新特性:video与audio的使用

    HTML5 DOM 为 <audio> 和 <video> 元素提供了方法.属性和事件. 这些方法.属性和事件允许您使用 JavaScript 来操作 <audio> ...

  9. 使用Jsoup解析出html中的img元素

    jsoup 是一款Java 的HTML解析器,可直接解析某个URL地址.HTML文本内容.它提供了一套非常省力的API,可通过DOM,CSS以及类似于jQuery的操作方法来取出和操作数据. 博客项目 ...

  10. SQL Server日期计算

    通常,你需要获得当前日期和计算一些其他的日期,例如,你的程序可能需要判断一个月的第一天或者最后一天.你们大部分人大概都知道怎样把日期进行分割(年.月.日等),然后仅仅用分割出来的年.月.日等放在几个函 ...