python读写配置文件操作示例
作者:轻舞肥羊 发布时间:2021-12-12 03:51:29
标签:python,读写配置文件
本文实例讲述了python读写配置文件操作。分享给大家供大家参考,具体如下:
在用编译型语言写程序的时候,很多时候用到配置文件,作为一个约定的规则,一般用 ini 文件作为配置文件,当然不是绝对的,也可能是XML等文件。
配置文件是配置的参数是在程序启动,或运行时需要的,作为编译型语言,几乎都会用到,但python是动态语言。动态语言的一大特性是解析执行的。所以很多情况下需要配置的参数,通常会被直接写在脚本里。一个常用的做法,就是单独用一个文件来作为配置文件,比如我们经常接触的 django ,他会用 settings.py ,urls.py 来配置一些参数。在需要修改的时候,直接修改这个 py 文件就可以了。
即使是这样,python 仍然提供了,读取配置文件的方法。在与其他系统结合的时候,通常会用得着。查看文档,自己实现了一个比较通用的读写配置文件的方法
# -*- coding:utf-8 -*-
import ConfigParser
import os
class ReadWriteConfFile:
currentDir=os.path.dirname(__file__)
filepath=currentDir+os.path.sep+"inetMsgConfigure.ini"
@staticmethod
def getConfigParser():
cf=ConfigParser.ConfigParser()
cf.read(ReadWriteConfFile.filepath)
return cf
@staticmethod
def writeConfigParser(cf):
f=open(ReadWriteConfFile.filepath,"w");
cf.write(f)
f.close();
@staticmethod
def getSectionValue(section,key):
cf=ReadWriteConfFile.getConfigParser()
return cf.get(section, key)
@staticmethod
def addSection(section):
cf=ReadWriteConfFile.getConfigParser()
allSections=cf.sections()
if section in allSections:
return
else:
cf.add_section(section)
ReadWriteConfFile.writeConfigParser(cf)
@staticmethod
def setSectionValue(section,key,value):
cf=ReadWriteConfFile.getConfigParser()
cf.set(section, key, value)
ReadWriteConfFile.writeConfigParser(cf)
if __name__ == '__main__':
ReadWriteConfFile.addSection( 'messages')
ReadWriteConfFile.setSectionValue( 'messages','name','sophia')
x=ReadWriteConfFile.getSectionValue( 'messages','1000')
print x
在你的 py 脚本下你创建一个 inetMsgConfigure.ini 文件,然后进行测试就可以了。如果inetMsgConfigure.ini 这个文件根本不存在,你当然可以调用python 的方法,创建一个文件
file=open('inetMsgConfigure.ini','wb')
file.write(.........自由发挥)
file.close()
希望本文所述对大家Python程序设计有所帮助。
来源:http://www.yihaomen.com/article/python/253.htm
0
投稿
猜你喜欢
- Python not equal operator returns True if two variables are of same ty
- 一、前言写这篇文章的灵感来源于我玩游戏的时候(为了避免过不了审就不说是啥游戏了),看见一个大佬在游戏里面建造了“还原方阵
- 查看数据库show databases;创建数据库create DATABASE 数据库名称create DATABASE database
- 详解python里使用正则表达式的分组命名方式分组匹配的模式,可以通过groups()来全部访问匹配的元组,也可以通过group()函数来按
- 目录1. Dash2. Pillow3. Colorama4. JmesPath5. Simplejson6. Emoji7. 进度条:pr
- 简介在SQL SERVER中,数据库在硬盘上的存储方式和普通文件在Windows中的存储方式没有什么不同,仅仅是几个文件而已.SQL SER
- 目录一、IDEA如何连接数据库第一种方法:直接在方法体中增加连接信息方法二:二、方法代码的实现1.快递员增加快递2.快递员删除快递用数据库编
- 数据结构typedef struct { PyObject_VAR_HEAD Py_ha
- 1.下载 4个rpm包mysql-community-client-5.7.26-1.el7.x86_64.rpmmysql-communi
- 要用django的orm表达sql的exists子查询,是个比较麻烦的事情,需要做两部来完成from django.db.models im
- 一起开发项目的时候总是要搭建环境和部署环境的,这个时候必须得有个python第三方包的list,一般都叫做requirements.txt。
- Django的Field类中方法有:to_python() # 把数据库数据转成python数据from_db_value() # 把数据库
- 能评估使用方法性能评估模块提供了一系列用于模型性能评估的函数,这些函数在模型编译时由metrics关键字设置性能评估函数类似与目标函数, 只
- python类私有属性和公共属性对于python而言,类的属性的可见度只有两种,public和private。类的私有属性便是在前面加上&a
- 在印刷排版中“point”是一个绝对的单位,它等于 1/72 英寸。可以用尺子丈量的,物理的英寸。但在CSS中pt的含义
- python使用ctypes模块调用windows api GetVersionEx获取当前系统版本,没有使用python32#!c:/py
- 我就废话不多说了,大家还是直接看代码吧!#先定义两个矩阵X=np.array([[1,2104,5,1,45],[1,1416,3,2,40
- 本文实例讲述了PHP面向对象程序设计类的定义与用法。分享给大家供大家参考,具体如下:<?phpclass Person {  
- 首先,创建一个DataFrame格式数据作为举例数据。# 创建一个DataFrame格式数据data = {'a': [
- <?php /** * Created by JetBrains Ph