config读取操作
cf = configparser.ConfigParser()    # 实例化对象
cf.read(filename)    # 读取文件
cf.sections()    # 读取sections值, 返回一个list
cf.options(sections)    # 读取options值, 返回一个list
cf.items(sections)    # 读取指定sections下所有的键值对, 返回list
cf.get(sections, key)    # 获取指定sections下指定key的value值, 返回str
cf.getint(sections, key)    # 获取指定sections下指定key的value值, 返回int
# encoding: utf-8

import configparser
import os class ReadConifg():
"""读取配置文件"""
def __init__(self, filename, filepath):
os.chdir(filepath) # 将当前工作目录切换到指定的目录
self.cf = configparser.ConfigParser() # 实例化configparser对象
self.cf.read(filename) # 读取文件 def read_sections(self):
"""读取配置文件中sections"""
sacs = self.cf.sections() # 获取sections,返回list
print("sections:", sacs, type(sacs)) def read_options(self, sections):
"""获取配置文件中的options值"""
opts = self.cf.options(sections) # 获取db section下的options,返回list
print("%s:%s" % (sections, opts), type(opts)) def read_kv(self, sections):
"""获取配置文件中的所有键值对"""
kvs = self.cf.items(sections) # 获取db section下的所有键值对,返回list
print("%s:%s" % (sections, kvs)) def get_str(self, sections, key):
"""获取指定sectons中指定的key的value值, 返回的会是 str 类型"""
value_str = self.cf.get(sections, key)
return value_str def get_int(self, sections, key):
"""获取指定sectons中指定的key的value值, 返回的会是 int 类型"""
value_int = self.cf.getint(sections, key)
return value_int

读取config文件

config写入操作

cf = configparser.ConfigParser()    # 实例化对象
cf.read(self.filename)    # 读取文件,如果是重新写入,覆盖原有内容不需要读取 
cf.add_section(section)    # 添加sections值
cf.set(section, option, value)    # 在指定的sections中添加键值对
cf.remove_section(section)    # 移除sections, 需要先cf.read(filename)
cf.remove_option(section, option)    # 移除指定sections下的options, 需要先cf.read(filename)
class WriteConfig():
"""写入config文件"""
def __init__(self, filename, filepath=r"D:\python_file\boke\config"):
self.filename = filename
os.chdir(filepath)
self.cf = configparser.ConfigParser()
self.cf.read(self.filename) # 如果修改,则必须读原文件 def _with_file(self):
# write to file
with open(self.filename, "w+") as f:
self.cf.write(f) def add_section(self, section):
# 写入section值
self.cf.add_section(section)
self._with_file() def set_options(self,section, option, value=None):
"""写入option值"""
self.cf.set(section, option, value)
self._with_file() def remove_section(self, section):
"""移除section值"""
self.cf.remove_section(section)
self._with_file() def remove_option(self, section, option):
"""移除option值"""
self.cf.remove_option(section, option)
self._with_file()

写入config文件

												

python向config、ini文件读取写入的更多相关文章

  1. INI文件的写入与读取

    INI文件的写入与读取 [节名]         '[]中的节名对应此API的第一参数 Name=内容      'Nmae对应此API的第二参数 API的第三参数是没有取到匹配内容时返回的字符串; ...

  2. 轮子:读取config.ini文件

    python: 把config.ini文件成map返回 def get_conf(conf_file): conf = {} ll=list(map(lambda x: x.replace('&quo ...

  3. C#对config.ini文件进行读取和修改

    C#对config.ini文件进行读取和修改: public partial class Patrolcar : Form之后可以加入如下类: #region public class IniFile ...

  4. PostgreSql那点事(文件读取写入、命令执行的办法)

    • 2013/07/9 作者: admin PostgreSql那点事(文件读取写入.命令执行的办法) 今天无意发现了个PostgreSQL环境,线上学习了下,一般的数据注射(读写数据库)差异不大,不 ...

  5. 1. 在config.ini文件中加入dm.park.time=1,会使uap中的tomcat启动加快

    在config.ini文件中加入dm.park.time=1,会使uap中的tomcat启动加快

  6. python中readline判断文件读取结束的方法

    注:内容来自网络 本文实例讲述了python中readline判断文件读取结束的方法.分享给大家供大家参考.具体分析如下: 大家知道,python中按行读取文件可以使用readline函数,下面现介绍 ...

  7. 91.生成ini文件并写入和读取ini文件

    写入 WritePrivateProfileStringA("hello money", infx[i].name, money, "1.ini"); 按照字符 ...

  8. C# 创建INI文件,写入并可读取。----转载

    基于C#winform设计. 首先创建一个类,我命名为IniFiles.并引入命名空间using System.Runtime.InteropServices; 接着,声明API函数 [DllImpo ...

  9. 【代码】ini 文件读取工具类

    using System; using System.Runtime.InteropServices; using System.Text; namespace hrattendance.Common ...

随机推荐

  1. android studio连接真机大概问题

    首先,确定手机Android的版本(最好用统一版本) 版本一样的话错误会少一点... 手机打开开发者选项(每个手机不同,百度可查) 开发者选项中打开USB调试 点击OK,可以进入Android mon ...

  2. 让Entity Framework不再私闯sys.databases

    这里的“私闯sys.databases”是指Entity Framework默认发起的查询:SELECT Count(*) FROM sys.databases WHERE [name]=N'数据库名 ...

  3. vue day7 table checkbox 全选

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  4. socks5服务器编写经验总结

    一.Socks5服务器实现设计 本Socks5服务器是之前做的一个项目中的一个小部分东西,该项目是一个可以实现多级转发代理网络通讯的项目,能够隐藏网络数据包的源IP地址和端口,能够为上网的用户提供安全 ...

  5. pip安装报错 解决办法

    安装库时报错:Could not fetch URL https://pypi.python.org/simple/wheel/: 解决办法:  pip --trusted-host pypi.pyt ...

  6. Vue面试中经常会被问到的面试题

    一.对于MVVM的理解 MVVM是 Model-View-ViewModel 的缩写. Model代表数据模型,也可以在Model中定义数据修改和操作的业务逻辑. View代表UI组件,它负责将数据模 ...

  7. 单机安装EFK(一)

    官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/getting-started-install.html#ge ...

  8. cocos2dx lua invalid 'cobj' in function 'lua_cocos2dx'

    解决方法 在创建 Node节点后 调用父节点 retain() 方法 手动增加引用 一般调用:clone()方法会出现,在变量后面加上对一个对应的retain() 方法

  9. MARK DOWN 书写格式说明

    MarkdownPad2 书写格式说明: Markdown是一种可以使用普通文本编辑器编写的标记语言,通过简单的标记语法,它可以使普通文本内容具有一定的格式,而MarkdownPad2是其中一种支持M ...

  10. 用anaconda安装tensorflow

    conda create -n tensorflow python=2.7 conda activate tensorflow / source activate tensorflow anacond ...