# -*- 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. sql 内联,左联,右联,全联

    联合查询效率较高,以下例子来说明联合查询(内联.左联.右联.全联)的好处: T1表结构(用户名,密码) userid (int) username varchar(20) password  varc ...

  2. 生成Word/ATU报表提示 font family not found

    1.先从你本机 C:\Windows\Fonts 拷贝或者网络上下载你想要安装的字体文件(*.ttf文件)到 /usr/share/fonts/chinese/TrueType 目录下(如果系统中没有 ...

  3. accept系统调用

    /* * For accept, we attempt to create a new socket, set up the link * with the client, wake up the c ...

  4. C#抓取网络图片保存到本地

    C#抓取网络图片保存到本地 System.Net.WebClient myWebClient = new System.Net.WebClient(); //将头像保存到服务器 string virP ...

  5. 洛谷P2279消防局的设立

    传送门啦 一个很摸不清头脑的树形dp 状态: $ dp[i][0] $ :选自己 $ dp[i][1] $ :选了至少一个儿子 $ dp[i][2] $ :选了至少一个孙子 ------------- ...

  6. 最近公共祖先(LCA)模板

    以下转自:https://www.cnblogs.com/JVxie/p/4854719.html 首先是最近公共祖先的概念(什么是最近公共祖先?): 在一棵没有环的树上,每个节点肯定有其父亲节点和祖 ...

  7. Java I/O系列汇总

    1.Java I/O---概述 2.Java I/O---File类 3.Java I/O---获取文件目录并写入到文本 4.Java I/O---输入与输出 5.Java I/O---复制文本文件 ...

  8. c语言双向循环链表

    双向循环链表,先来说说双向链表,双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱.所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继 ...

  9. TeamViewer的下载地址,低调低调

    https://github.com/cary-zhou/TeamViewer13-Crack

  10. Ansible之迭代、模板

    本节内容: 迭代 模板(JInjia2相关) Jinja2相关 一.迭代 当有需要重复性执行的任务时,可以使用迭代机制.其使用格式为将需要迭代的内容定义为item变量引用,并通过with_items语 ...