120prop-python3.7 读写.properties文件
120prop-python3.7 读写.properties文件
[an error occurred while processing the directive] [an error occurred while processing the directive]
#!/usr/bin/python
# -*- coding: UTF-8 -*- import re
import os
import tempfile class Properties: def __init__(self, file_name):
self.file_name = file_name
self.properties = {}
try:
fopen = open(self.file_name, 'r')
for line in fopen:
line = line.strip()
if line.find('=') > 0 and not line.startswith('#'):
strs = line.split('=')
self.properties[strs[0].strip()] = strs[1].strip()
except Exception :
raise e
else:
fopen.close() def has_key(self, key):
return key in self.properties def get(self, key, default_value=''):
if key in self.properties:
return self.properties[key]
return default_value def put(self, key, value):
self.properties[key] = value
replace_property(self.file_name, key + '=.*', key + '=' + value, True) def replace_property(file_name, from_regex, to_str, append_on_not_exists=True):
tmpfile = tempfile.TemporaryFile() if os.path.exists(file_name):
r_open = open(file_name, 'r')
pattern = re.compile(r'' + from_regex)
found = None
for line in r_open:
if pattern.search(line) and not line.strip().startswith('#'):
found = True
line = re.sub(from_regex, to_str, line)
tmpfile.write(line.encode())
if not found and append_on_not_exists:
tmpfile.write(('\n' + to_str).encode())
r_open.close()
tmpfile.seek(0) content = tmpfile.read() if os.path.exists(file_name):
os.remove(file_name) w_open = open(file_name, 'wb')
w_open.write(content)
w_open.close() tmpfile.close()
else:
print ("file %s not found" % file_name) if __name__ == "__main__":
file_path = 'jdbc.properties'
props = Properties(file_path) # 读取文件
props.put('jdbc.url', 'value_a') # 修改/添加key=value
print (props.get('key_a')) # 根据key读取value
print ("props.has_key('key_a')=" + str(props.has_key('key_a'))) # 判断是否包含该key
120prop-python3.7 读写.properties文件的更多相关文章
- Java读写.properties文件实例,解决中文乱码问题
package com.lxk.propertyFileTest; import java.io.*; import java.util.Properties; /** * 读写properties文 ...
- 读写properties文件方法
按key读取properties文件中的value public static String readSystemConfig(String key){ Properties prop = new P ...
- 读写properties文件
1. 读properties文件 Properties props = new Properties(); try { InputStream in = new FileInputStream(&qu ...
- java读写Properties文件
Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ...
- 用java读写properties文件的代码
package com.LY; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.F ...
- Java读写资源文件类Properties
Java中读写资源文件最重要的类是Properties 1) 资源文件要求如下: 1.properties文件是一个文本文件 2.properties文件的语法有两种,一种是注释,一种属性配置. 注 ...
- Properties读写资源文件
Java中读写资源文件最重要的类是Properties,功能大致如下: 1. 读写Properties文件 2. 读写XML文件 3. 不仅可以读写上述两类文件,还可以读写其它格式文件如txt等,只要 ...
- Python:解析properties文件
在项目中遇到解析properties的情况,而Python中正好没有解析properties文件的现成模块,于是从网上找到了这个脚本,有一些小地方修改了一下 原博客: Python读写properti ...
- java 读写properties (配置)文件
Properties属性文件在Java应用程序中是经常可以看得见的,也是特别重要的一类文件.它用来配置应用程序的一些信息,不过这些信息一般都是比较少的数据,没有必要使用数据库文件来保存,而使用一般的文 ...
随机推荐
- RHEL系统下安装atlassian-jira-5
操作系统:RHEL 6.4 x86_64 Jira版本:atlassian-jira-5.2.11-x64.bin 安装路径:/opt/atlassian/jira/ 数据保存路径:/opt/atla ...
- C++扬帆远航——18(项目五2,递归式)
/* * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:qiushi.cpp * 作者:常轩 * 微信公众号:World ...
- C++走向远洋——59(项目三、图形面积、抽象类)
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...
- JS逆向某网站登录密码分析
声明: 本文仅供研究学习使用,请勿用于非法用途! 目标网站 aHR0cHM6Ly9hdXRoLmFsaXBheS5jb20vbG9naW4vaW5kZXguaHRt 今日目标网站是某知名支付网站,感觉 ...
- Java程序员常用的@Component、@Repository、@Controller、@Service系列【案例demo3】
Java程序员常用的@Component.@Repository.@Controller.@Service系列[案例demo3] 很多程序员通过在类上使用@Repository.@Componen ...
- 设计模式-12组合模式(Composite Pattern)
1.模式动机 很多时候会存在"部分-整体"的关系,例如:大学中的部门与学院.总公司中的部门与分公司.学习用品中的书与书包.在软件开发中也是这样,例如,文件系统中的文件与文件夹.窗体 ...
- 利用canvas绘画二级树形结构图
上周需要做一个把页面左侧列表内容拖拽到右侧区域,并且绘制成关系树的功能.看了设计图,第一反应是用canvas绘制关系线.吭哧吭哧搞定这个功能后,发现用canvas绘图,有一个很严重的缺陷.那就是如果左 ...
- Pycharm IDE安装及注册激活笔记(1)
一.Windows 下的安装及激活. 1.首先去Pycharm官网,或者直接输入网址:http://www.jetbrains.com/pycharm/download/#section=window ...
- 解决Hexo博客模板hexo-theme-next的翻页按钮不正常显示问题
用Hexo搭了个Gitpage的博客,兴冲冲的发了11篇博文后发现翻页按钮不正常显示,显示为<i class="fa fa-angle-right"></i> ...
- Django 图片上传到数据库 并调用显示
环境:Django2.0 Python3.6.4 建立项目,数据库设置,就不说了. 直接上代码: 在models.py中,需要建立模型,这里使用了ImageField字段,用来存储图片路径,这个字段继 ...