python对配置文件.ini进行增删改查操作的方法示例
作者:leason-love 时间:2023-10-25 05:56:13
前言
本文主要给大家介绍的是关于python对配置文件.ini增删改查操作的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍:
一、先导入configobj库文件
可以用pip直接安装
#!/usr/bin/python
# -*- coding: utf-8 -*-
import json
from configobj import ConfigObj
二、增添section
这里是前后端分离的例子,从前端接收json数据,然后写入配置文件中
def add(self, false=None):
self.log.debug("list")
try:
conf_ini = CONFIG_INI_PATH+"users.ini.bak"
config = ConfigObj(conf_ini, encoding='UTF8')
req = self.input["input"]
data = req["data"]
userName = data["userName"]
disc = data["disc"]
ip = data["ip"]
expMonth = int(float(data["expDate"]) * 12)
for user in config.items():
if userName == user[0]:
self.out = '{"status": 1,"msg":"用户名已存在!"}'
return false
else:
pass
config[userName] = {}
config[userName]['user'] = userName
config[userName]['disc'] = disc
config[userName]['ip'] = ip
config[userName]['validity_date'] = data["expDate"]
config[userName]['cert_expired'] = get_today_month(expMonth)
config[userName]['enable'] = 0
config[userName]['path'] = USER_KEY_PATH + userName
config.write()
self.out = '{"status": 0,"msg":"操作成功!"}'
except Exception, e:
self.out = '{"status":1, "msg":"'+str(e)+'"}'
三、修改section
def modify(self):
self.log.debug("modify")
try:
conf_ini = CONFIG_INI_PATH + "users.ini.bak"
config = ConfigObj(conf_ini, encoding='UTF8')
req = self.input["input"]
data = req["data"]
userName = data["userName"]
disc = data["disc"]
ip = data["ip"]
config[userName]['disc'] = disc
config[userName]['ip'] = ip
config.write()
self.out = '{"status": 0,"msg":"操作成功!"}'
except Exception, e:
self.out = '{"status":1, "msg":"'+str(e)+'"}'
四、删除section
通过section名找到相应section进行del操作
def delete(self, false=None):
self.log.debug("delete")
try:
conf_ini = CONFIG_INI_PATH + "users.ini.bak"
config = ConfigObj(conf_ini, encoding='UTF8')
req = self.input["input"]
data = req["data"]
userName = data["userName"]
for user in config.items():
if userName == user[0]:
del config[userName]
config.write()
self.out = '{"status": 0,"msg":"操作成功!"}'
return false
else:
pass
self.out = '{"status": 1,"msg":"用户不存在!"}'
except Exception, e:
self.out = '{"status":1, "msg":"config err!"}'
五、查询section
这里借用python字典将配置文件里的内容整体输出,代码里还有查询和分页的功能
def list(self):
self.log.debug("list")
try:
req = self.input["input"]
data = req["data"]
pageSize = req["pageSize"]
pageIndex = req["pageIndex"]
userName = data["userName"]
conf_ini = CONFIG_INI_PATH + "users.ini.bak"
config = ConfigObj(conf_ini, encoding='UTF8')
users = []
n = 0
if userName == '':
for user in config.items():
n = n + 1
if pageSize * pageIndex + 1 <= n <= pageSize * (pageIndex + 1):
users.append(user[1])
else:
pass
else:
for user in config.items():
if userName == user[0]:
n = n + 1
if pageSize * pageIndex + 1 <= n <= pageSize * (pageIndex + 1):
users.append(user[1])
else:
pass
else:
pass
utext = json.dumps(users)
self.out = '{"status": 0,"total":'+str(n)+',"data":' + utext + '}'
except Exception, e:
self.out = '{"status":1, "msg":"' + str(e) + '"}'
self.log.debug("list in.")
来源:http://blog.csdn.net/qq_29287973/article/details/52446770
标签:python,.ini配置文件,增删改查
0
投稿
猜你喜欢
详解python数据结构和算法
2021-06-21 13:48:19
Python操作注册表详细步骤介绍
2023-09-16 01:05:53
pyqt5让图片自适应QLabel大小上以及移除已显示的图片方法
2022-02-26 16:18:37
在Python中使用lambda高效操作列表的教程
2023-08-02 04:42:53
pandas 中对特征进行硬编码和onehot编码的实现
2021-03-26 22:37:49
MySQL主从同步原理介绍
2024-01-13 08:01:43
TensorFlow如何实现反向传播
2023-07-04 08:44:36
如何用python反转图片,视频
2023-08-27 08:18:21
PHP+jQuery翻板抽奖功能实现
2024-05-05 09:17:43
Go 高效截取字符串的一些思考
2024-04-30 10:04:43
PHP错误Allowed memory size of 67108864 bytes exhausted的3种解决办法
2024-03-15 18:25:42
sql server自动生成拼音首字母的函数
2024-01-25 15:27:19
python读取指定字节长度的文本方法
2022-04-17 10:42:45
一看就懂得Python的math模块
2021-03-28 21:40:32
Oracle修改表空间大小的方法
2024-01-13 04:14:49
python自动化测试selenium操作checkbox和radiobox技术
2021-11-01 16:12:54
python3爬取torrent种子链接实例
2022-03-05 11:23:44
python如何为创建大量实例节省内存
2023-03-19 06:35:37
JavaScript 数据结构之字典方法
2024-04-16 09:28:22
asp.net性能的技巧
2007-10-07 21:55:00