python 读写配置文件
使用python读写配置文件,写个demo测试一下。
#!/usr/bin/env python
import os
import ConfigParser
# 如果文件不存在,就穿件文件。
if os.path.isfile(os.getcwd() + '/sql.conf') == False:
cfg = ConfigParser.ConfigParser()
cfg.add_section('remote') # 添加section
cfg.set('remote', 'ip' ,'192.168.3.123') # 添加字段
cfg.set('remote', 'port' ,'40949')
cfg.set('remote', 'user' ,'tony')
cfg.set('remote', 'password' ,'tony')
cfg.set('remote', 'table' ,'real_tabel')
cfg.add_section('localhost')
cfg.set('localhost', 'ip' ,'127.0.0.1')
cfg.set('localhost', 'port' ,'3306')
cfg.set('localhost', 'user' ,'root')
cfg.set('localhost', 'password' ,'root')
cfg.add_section('interval')
cfg.set('interval', 'heartbeat', '1')
cfg.set('interval', 'upload_data', '5')
cfg.write(open(os.getcwd() + '/sql.conf', 'w+'))
# 文件存在就打印出来
else:
cfg = ConfigParser.ConfigParser()
cfg.read(os.getcwd() + '/sql.conf')
print cfg.items('remote') # 获取remote中的所有内容,返回字典。
print cfg.get('remote', 'ip') # 获取remote中,ip的值
print cfg.getint('remote', 'port') # 获取remote中,port的值,并转为int型
print cfg.get('remote', 'user')
print cfg.get('remote', 'password')
print cfg.get('remote', 'table')
print cfg.items('localhost')
print cfg.get('remote', 'ip')
print cfg.getint('remote', 'port')
print cfg.get('remote', 'user')
print cfg.get('remote', 'password')
print cfg.items('interval')
print cfg.get('interval', 'heartbeat')
print cfg.get('interval', 'upload_data')
第一次运行创建文件
[remote]
ip = 192.168.3.123
port = 40949
user = tony
password = tony
table = real_tabel
[localhost]
ip = 127.0.0.1
port = 3306
user = root
password = root
[interval]
heartbeat = 1
upload_data = 5
第二次输出结果。
[('ip', '192.168.3.123'), ('port', '40949'), ('user', 'tony'), ('password', 'tony'), ('table', 'real_tabel')]
192.168.3.123
40949
tony
tony
real_tabel
[('ip', '127.0.0.1'), ('port', '3306'), ('user', 'root'), ('password', 'root')]
192.168.3.123
40949
tony
tony
[('heartbeat', '1'), ('upload_data', '5')]
1
5
Tony Liu
2017-6-9, Shenzhen
python 读写配置文件的更多相关文章
- python读写配置文件使用总结与避坑指南
关于今天的内容 最近拿python在写项目部署的相关集成代码,本来两天的工作量,硬是在来回的需求变更中,拖到了一周的时间.今天算是暂时告一段落了.这次由于涉及多个系统的调用和配置参数,代码开发中出现了 ...
- Python读写配置文件模块--Configobj
一.介绍 我们在项目的开发过程中应该会遇到这样的问题:我们的项目读取某个配置文件,然后才能按照配置的信息正常运行服务,当我们需要对修改服务的某些信息时,可以直接修改这个配置文件,重启服务即可,不用再去 ...
- python读写配置文件
#coding:utf-8 import ConfigParser class Conf(): def __init__(self,name): self.name = name self.cp = ...
- python-ConfigParser模块【读写配置文件】
对python 读写配置文件的具体方案的介绍 1,函数介绍 import configParser 如果Configparser无效将导入的configParser 的C小写 1.1.读取配置文件 - ...
- 【python】配置文件
来源:http://developer.51cto.com/art/201003/189885.htm python 读写配置文件在实际应用中具有十分强大的功能,在实际的操作中也有相当简捷的操作方案, ...
- Python-通过configparser读写配置文件
Python读写配置文件: 1.创建配置文件(文件名以.conf或.ini结束的文件表示配置文件) 2.导入所需模块 OS, configparser >>> import os & ...
- 用ConfigParser模块读写配置文件——Python
对于功能较多.考虑用户体验的程序,配置功能是必不可少的,如何存储程序的各种配置? 1)可以用全局变量,不过全局变量具有易失性,程序崩溃或者关闭之后配置就没了,再者配置太多,将变量分配到哪里也是需要考虑 ...
- Python自动化测试 (二) ConfigParser模块读写配置文件
ConfigParser 是Python自带的模块, 用来读写配置文件, 用法及其简单. 直接上代码,不解释,不多说. 配置文件的格式是: []包含的叫section, section 下有op ...
- Python自动化测试 -ConfigParser模块读写配置文件
C#之所以容易让人感兴趣,是因为安装完Visual Studio, 就可以很简单的直接写程序了,不需要做如何配置. 对新手来说,这是非常好的“初体验”, 会激发初学者的自信和兴趣. 而有些语言的开发环 ...
随机推荐
- C和指针第13章第4题
题目:编写一个函数,它用于对一个任何类型的数组进行排序. 算法核心代码sort函数实现 /** 功能说明:sort函数可以对不同类型的数据进行排序 参数: 1.一个指向需要排序的数组的第一个值的指针. ...
- OpenStack的基本概念与架构图
https://blog.csdn.net/zjluobing/article/details/51489325 OpenStack项目是一个开源的云计算平台,旨在实现很简单,大规模可伸缩,功能丰富. ...
- 36氪首发 | 「myShape」完成千万级人民币 Pre-A轮融资,推出 AI 智能健身私教
无需任何可穿戴设备. 36氪获悉,myShape(原Shapejoy)已于近期完成千万级人民币的Pre-A轮融资,由天奇阿米巴领投,远洋集团.七熹资本以及老股东跟投.过去 myShape 曾获得元迅资 ...
- 【驱动】网卡驱动·linux内核网络分层结构
Preface Linux内核对网络驱动程序使用统一的接口,并且对于网络设备采用面向对象的思想设计. Linux内核采用分层结构处理网络数据包.分层结构与网络协议的结构匹配,既能简化数据包处理流程 ...
- openfire维持在线状态,监听消息
public static void testLoginStatus()throws XMPPException,InterruptedException { AccountManager accou ...
- Beginning SDL 2.0(6) 音频渲染及wav播放
前面几篇关于SDL的文章介绍的是以画面为主,这里介绍下SDL中针对音频播放提供的机制,以及如何应用. 对于音频而言,有几个概念需要事先了解下,采样率.声道数.量化位数,如果你不清楚的话,麻烦先了解下这 ...
- $.ajax使用总结(一):Form提交与Payload提交
http://blog.csdn.net/yiifaa/article/details/73468001 *********************************************** ...
- Oracle sql%rowcount 返回影响行数;sql server @@RowCount返回影响行数
sql server中,返回影响行数是:If @@RowCount<1 Oracle中,返回影响行数是:If sql%rowcount<1 例: sqlserver: create pro ...
- 【转】ExtJS获取父子、兄弟容器元素方法
原文地址:http://www.cnblogs.com/linxiong945/p/3961732.html 1.当前对象的父对象(上级对象) this.ownerCt: 2.当前对象的下一个相邻的对 ...
- 【emWin】例程三十三:窗口对象———Radio
简介: 如同复选框一样,单选按钮也可用来选择选项.打开或选择单选按钮时,将出现 圆点.与复选框的差别是,用户一次只能选择一个单选按钮.选择一个按钮时,小工 具中的其他按钮将关闭. 触摸校准(上电可选择 ...