python向config、ini文件读取写入
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文件读取写入的更多相关文章
- INI文件的写入与读取
INI文件的写入与读取 [节名] '[]中的节名对应此API的第一参数 Name=内容 'Nmae对应此API的第二参数 API的第三参数是没有取到匹配内容时返回的字符串; ...
- 轮子:读取config.ini文件
python: 把config.ini文件成map返回 def get_conf(conf_file): conf = {} ll=list(map(lambda x: x.replace('&quo ...
- C#对config.ini文件进行读取和修改
C#对config.ini文件进行读取和修改: public partial class Patrolcar : Form之后可以加入如下类: #region public class IniFile ...
- PostgreSql那点事(文件读取写入、命令执行的办法)
• 2013/07/9 作者: admin PostgreSql那点事(文件读取写入.命令执行的办法) 今天无意发现了个PostgreSQL环境,线上学习了下,一般的数据注射(读写数据库)差异不大,不 ...
- 1. 在config.ini文件中加入dm.park.time=1,会使uap中的tomcat启动加快
在config.ini文件中加入dm.park.time=1,会使uap中的tomcat启动加快
- python中readline判断文件读取结束的方法
注:内容来自网络 本文实例讲述了python中readline判断文件读取结束的方法.分享给大家供大家参考.具体分析如下: 大家知道,python中按行读取文件可以使用readline函数,下面现介绍 ...
- 91.生成ini文件并写入和读取ini文件
写入 WritePrivateProfileStringA("hello money", infx[i].name, money, "1.ini"); 按照字符 ...
- C# 创建INI文件,写入并可读取。----转载
基于C#winform设计. 首先创建一个类,我命名为IniFiles.并引入命名空间using System.Runtime.InteropServices; 接着,声明API函数 [DllImpo ...
- 【代码】ini 文件读取工具类
using System; using System.Runtime.InteropServices; using System.Text; namespace hrattendance.Common ...
随机推荐
- 20164322 韩玉婷-----Exp6 信息搜索与漏洞扫描
1.实践目标 掌握信息搜集的最基础技能与常用工具的使用方法. 2.实践内容 (1)各种搜索技巧的应用 (2)DNS IP注册信息的查询 (3)基本的扫描技术:主机发现.端口扫描.OS及服务版本探测.具 ...
- 2018-2019-2 20165313 《网络对抗技术》Exp4 恶意代码分析
一.实践目标 1.监控你自己系统的运行状态,看有没有可疑的程序在运行. 2.分析一个恶意软件,就分析Exp2或Exp3中生成后门软件:分析工具尽量使用原生指令或sysinternals,systrac ...
- sklearn learn preprocessing
train_test_split sklearn.model_selection.train_test_split(*arrays, test_size(float,int/None),#defaul ...
- Mac OS环境下DOSBox汇编环境的搭建
平台: mac 工具: DOSBox(点击下载DOSBox官网) debug.edit.link.masm等等(点击下载masm汇编开发工具,提取码: skc8) 步骤: 1 .安装DOSBox: 解 ...
- Oracle的安装+PL安装+系统变量配好后重启
服务启动后的样子 第一步安装oracle服务 链接: https://pan.baidu.com/s/1sRu95Vy1arc3gfuH9nH5Wg 提取码: eaxx 复制这段内容后打开百度网盘手机 ...
- TensorFlow机器学习实战指南之第一章
TensorFlow基础 一.TensorFlow算法的一般流程 1.导入/生成样本数据集 2.转换和归一化数据:一般来讲,输入样本数据集并不符合TensorFlow期望的形状,所以需要转换数据格式以 ...
- Python学习:列表、元组、字典、集合
转载:https://www.cnblogs.com/xc-718/p/9632942.html 列表/元组 列表和元组都是序列结构,它们本身很相似,但又有一点不同: 列表是用方括号标记,如:a=[1 ...
- C# .NET 按ASCII 从小到大排序
//C#的SortedDictionary<string,string>集合对key不是按照ascii码排序的因为他没有区分大小写,这就是个差别. 如果参数名中间有大写,小写,数字,Sor ...
- Ubuntu 18.04 启用 rc.local 设置开机启动
ubuntu18.04 不再使用initd管理系统,改用systemd. 然而systemd很难用,改变太大,跟之前的完全不同. 使用systemd设置开机启动为了像以前一样,在/etc/rc.loc ...
- 阿里云CentOS7服务器利用LVM分区挂载磁盘全记录
1.进入服务器后,首先利用fdisk -l来观察磁盘信息,可以看出红线标注处,有两块硬盘信息,分别是40G和300G 2.同时你也可以观察到分区信息,40G的硬盘已经分了一个区vda1,大小(Bloc ...