Python实用脚本(1):读取Properties文件
2015-07-28 14:56
801 查看
JAVA本身提供了对于Properties文件操作的类,项目中的很多配置信息都是放在了Properties文件。但是Python并没有提供操作Properties文件的库,所以,自己动手写个一个可以加载Properties文件的脚本。
class Properties:
fileName = ''
def __init__(self, fileName):
self.fileName = fileName
def getProperties(self):
try:
pro_file = open(self.fileName, 'r')
properties = {}
for line in pro_file:
if line.find('=') > 0:
strs = line.replace('\n', '').split('=')
properties[strs[0]] = strs[1]
except Exception, e:
raise e
else:
pro_file.close()
return properties
实际调用:
fileName = sys.path[0] + '\\'+ 'system.properties'
p = Properties(fileName)
properties = p.getProperties()
print properties[Key]
class Properties:
fileName = ''
def __init__(self, fileName):
self.fileName = fileName
def getProperties(self):
try:
pro_file = open(self.fileName, 'r')
properties = {}
for line in pro_file:
if line.find('=') > 0:
strs = line.replace('\n', '').split('=')
properties[strs[0]] = strs[1]
except Exception, e:
raise e
else:
pro_file.close()
return properties
实际调用:
fileName = sys.path[0] + '\\'+ 'system.properties'
p = Properties(fileName)
properties = p.getProperties()
print properties[Key]
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法
- Python实现的基于ADB的Android远程工具