python配置文件操作——configparser模块
# -*- 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模块的更多相关文章
- python:利用configparser模块读写配置文件
在自动化测试过程中,为了提高脚本的可读性和降低维护成本,将一些通用信息写入配置文件,将重复使用的方法写成公共模块进行封装,使用时候直接调用即可. 这篇博客,介绍下python中利用configpars ...
- Python 标准库 ConfigParser 模块 的使用
Python 标准库 ConfigParser 模块 的使用 demo #!/usr/bin/env python # coding=utf-8 import ConfigParser import ...
- Python自动化测试 (二) ConfigParser模块读写配置文件
ConfigParser 是Python自带的模块, 用来读写配置文件, 用法及其简单. 直接上代码,不解释,不多说. 配置文件的格式是: []包含的叫section, section 下有op ...
- python:实例化configparser模块读写配置文件
之前的博客介绍过利用python的configparser模块读写配置文件的基础用法,这篇博客,介绍下如何实例化,方便作为公共类调用. 实例化的好处有很多,既方便调用,又降低了脚本的维护成本,而且提高 ...
- Python语言的configparser模块便捷的读取配置文件内容
配置文件是在写脚本过程中经常会用到的,所以读取配置文件的模块configparser也非常重要,但是很简单. 首先我们的配置文件内容为: 这样的配置文件,[]里面的内容叫section,[]下面的内容 ...
- python学习之 - configparser模块
configparser模块功能:用于生成和修改常见配置文件.基本常用方法如下: read(filename):直接读取配置文件write(filename):将修改后的配置文件写入文件中.defau ...
- Python读写操作Excel模块_xlrd_xlwt_xlutils
Python 读写操作Excel -- 安装第三方库(xlrd.xlwt.xlutils.openpyxl) 如果仅仅是要以表单形式保存数据,可以借助 CSV 格式(一种以逗号分隔的表格数据格式)进行 ...
- python中的ConfigParser模块
1.简介 我们经常需要使用配置文件,例如.conf和.ini等类型,使用ConfigPaser模块可以对配置文件进行操作. 2.示例 现有配置文件test.ini,其内容如下: [section_a] ...
- python学习 day19 configparser模块 os模块 subprocess模块
上周五回顾 logging 用于记录日志 四种核心角色: 生成器Logger 过滤器Filter 处理器Handler 格式化处理器 Formatter logging.info.debug 使用默认 ...
随机推荐
- js API
从基础知识JS-web-API js基础知识:ECMA 262标准 js-web-API: w3c标准 W3c标准中关于js的规定有 DOM操作.BOM操作.事件绑定.ajax请求(包括http协议) ...
- poj1063
题意:有一些珠子排成一圈,珠子有两种颜色:黑和白.每次操作可以调换中间隔着一个珠子的两珠子的位置,给出这个圈子的初始状态,问最终能否通过操作让圈子中所有同色的珠子排在一起,即黑白分开. 分析:分两种情 ...
- 数据结构之队列(Python 版)
数据结构之队列(Python 版) 队列的特点:先进先出(FIFO) 使用链表技术实现 使用单链表技术,在表首尾两端分别加入指针,就很容易实现队列类. 使用顺序表list实现 # 队列类的实现 cla ...
- Spring MVC参数注入注意事项
1.类参数名不能出现‘name’ 2.需提供默认的无参构造
- Linux_僵尸进程、挂载、block块、inode号
僵尸进程: 基本概念: 进程分为父进程和子进程 父进程一死 子进程都会死 杀死主进程的时候 子进程也会被杀死 僵尸进程:主进程被杀死了 但是子进程还活着 子进程不会占用cpu但还是会占着内存 ...
- git clone命令使用
git clone命令使用 分类: 项目构建2013-06-26 15:43 38660人阅读 评论(2) 收藏 举报 GitClone git clone 命令参数: usage: git clon ...
- 洛谷P1242 新汉诺塔
传送门啦 首先要将第n个盘子从x到y,那么就要把比n小的盘子全部移到6-x-y,然后将n移到y 仔细想想:6代表的是3根初始柱,3根目标柱. 6-(x+y) 便是我们的中转柱了,因为到这个位置是最优的 ...
- CSS — 贝塞尔曲线(cubic-bezier)
cubic-bezier 又称三次贝塞尔,主要是为 animation 生成速度曲线的函数,规定是 cubic-bezier(<x1>, <y1>, <x2>, & ...
- java小爬虫
爬取煎蛋网 1.找出页面网址的规律 2.设计页面图片网址的正则 代码: import java.io.BufferedInputStream; import java.io.BufferedOutpu ...
- Redux-DevTools 安装
以下以Chrome为准. 首先,从Chrome Web Store(需要***支持)下载chrome 插件 Redux DevTools. 使用方式有两种: 一种只需在代码createStore中添加 ...