python读取配置文件configparser
来源:互联网 发布:蔡司编程视频教程 编辑:程序博客网 时间:2024/05/22 06:16
跟着 http://blog.csdn.net/u011541946/article/details/70174276 博主的博客,了解了一下configparser,自己扩展和百度 了一下写入文件和configparser一些常用方法
可读取写入配置文件
import configparserimport osimport sysclass TestCfigParser(object): config = configparser.ConfigParser() def get_value(self): root_dir = os.path.dirname(os.path.abspath('.')) print(root_dir) file_path = os.path.dirname(os.path.abspath('.'))+'/config/config.ini' #文件路径 self.config.read(file_path) #读文件 browser = self.config.get("browserType","browserName") #key、value类型 url = self.config.get("testServer","URL") for section in self.config.sections(): #尝试用到section、option print(section) for option in self.config.options(section):#k,value section \ option #print(option) print(option+'='+self.config.get(section,option)) #get方法get(section,option) return(browser,url) def set_value(self): file_path = os.path.dirname(os.path.abspath('.')) + '/config/config.ini' self.config.add_section('book') self.config.set('book','title','标题') self.config.set('book','author','作者') self.config.write(sys.stdout) self.config.write( open(file_path, 'a') )test = TestCfigParser()print(test.get_value())test.set_value()在python 3 中模块名字为configparser首字母小写,文章 http://blog.csdn.net/ztf312/article/details/47259805 提到一些常用的方法如下
1、config=configParser.ConfigParser()
创建ConfigParser实例
2、config.sections()
返回配置文件中节序列
3、config.options(section)
返回某个项目中的所有键的序列
4、config.get(section,option)
返回section节中,option的键值
5、config.add_section(str)
添加一个配置文件节点(str)
6、config.set(section,option,val)
设置section节点中,键名为option的值(val)
7、config.read(filename)
读取配置文件
8、config.write(obj_file)
写入配置文件
阅读全文
0 0
- python读取配置文件configparser
- Python利用ConfigParser读取配置文件
- Python 之 ConfigParser : 配置文件读取
- Python利用ConfigParser读取配置文件
- python之ConfigParser读取配置文件
- Python 读取写入配置文件 —— ConfigParser
- Python读取配置文件:ConfigParser.NoSectionError: No section:
- Python读取修改ini配置文件[ConfigParser]
- ConfigParser 读取 config 配置文件
- 使用python的ConfigParser模块方便的读取配置文件
- Python学习之读取配置文件模块ConfigParser详解
- PYTHON 配置文件模块 ConfigParser
- [Python]ConfigParser解析配置文件
- python ConfigParser操作配置文件
- 【python】ConfigParser---读写配置文件
- Python configparser修改配置文件
- 【python】configparser读写配置文件
- Python 配置文件 ConfigParser 模块
- Eclipse中tomcat的相关配置
- 01:与指定数字相同的数的个数
- OpenCV常用API
- [Navicat] 连接Oracle报错_Cannot load OCI DLL 87
- 使用YASM编程
- python读取配置文件configparser
- web之路
- 2018网易校招题
- 迷宫问题,最短路径
- 侧滑删除菜单 SwipeMenuLayout
- MFC中Radio Button使用方法
- C语言中全局变量、局部变量、静态全局变量、静态局部变量的区别
- Mean time to recovery/HBase
- linux网络编程资料整理