老前辈解析:python对配置文件.ini进行增删改查操作的方法

@本文来源于公众号:csdn2299,喜欢可以关注公众号 程序员学府
.ini配置文件常被用作存储程序中的一些参数,通过它程序可以变得更加灵活。下面这篇文章主要给大家介绍了关于python对配置文件.ini进行增删改查操作的方法示例,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。

文章目录

    • 前言
    • 一、先导入configobj库文件
    • 二、增添section
    • 三、修改section
    • 四、删除section
    • 五、查询section

前言

本文主要给大家介绍的是关于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.")
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.")

非常感谢你的阅读
大学的时候选择了自学python,工作了发现吃了计算机基础不好的亏,学历不行这是没办法的事,只能后天弥补,于是在编码之外开启了自己的逆袭之路,不断的学习python核心知识,深入的研习计算机基础知识,整理好了,我放在我们的微信公众号《程序员学府》,如果你也不甘平庸,那就与我一起在编码之外,不断成长吧!

其实这里不仅有技术,更有那些技术之外的东西,比如,如何做一个精致的程序员,而不是“屌丝”,程序员本身就是高贵的一种存在啊,难道不是吗?[点击加入]想做你自己想成为高尚人,加油!
添加链接描述

你可能感兴趣的:(python,python,人工智能,编程语言,深度学习)