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 使用默认 ...
随机推荐
- 建立ARM交叉编译环境 (arm-none-linux-gnueabi-gcc with EABI)【转】
转自:http://lib.csdn.net/article/embeddeddevelopment/60172?knId=886 建立ARM交叉编译环境 (arm-none-linux-gnueab ...
- C# 序列化高级用法
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...
- jQuery基本筛选器-表单筛选器-关系筛选器
一.基本筛选器 :first // 第一个 :last // 最后一个 :eq(index)// 索引等于index的那个元素 :even // 匹配所有索引值为偶数的元素,从 0 开始计数 :odd ...
- 002_Linux-Memory专题
一.单独查看某个进程的内存占用 pmap 736 | tail -n 1 二. 以前我对这块认识很模糊,而且还有错误的认识:今天由我同事提醒,所以我决定来好好的缕缕这块的关系. 图: -------- ...
- JS判断是否是PC端访问网站
function IsPC() { var userAgentInfo = navigator.userAgent; var Agents = ["Android", " ...
- MAVLink v1.0详解——结构
本文针对 MAVLink v1.0版本,协议版本:3. MAVLink是为微型飞行器MAV(Micro Air Vehicle)设计的(LGPL)开源的通讯协议.是无人飞行器和地面站(Ground C ...
- jenkins主从服务器部署
当服务器为linux系统但也有部分ios代码,此时就需要添加一个从jenkins以便编译ios代码.或者需要多个job同时编译这时就需要搭建主从服务器. 1.主(master)节点安装jenkins ...
- 洛谷P2261余数求和
传送门啦 再一次见证了分块的神奇用法,在数论里用分块思想. 我们要求 $ ans = \sum\limits ^{n} _{i=1} (k % i) $ ,如果我没看错,这个题的暴力有 $ 60 $ ...
- TObjectList
AOwnsObjects = true 就是 objectlist释放的时候,里面的对象一并释放. TObjectList对象的创建方法有一个参数:constructor TObjectList.C ...
- Ubuntu 搭建etcd
一.简介 etcd是一个高可用的分布式键值(key-value)数据库.etcd内部采用raft协议作为一致性算法,etcd基于Go语言实现. 提供配置共享和服务发现的系统比较多,其中最为大家熟知的是 ...