Python读写ini文件的方法

本文实例讲述了Python读写ini文件的方法。分享给大家供大家参考。具体如下:

比如有一个文件update.ini,里面有这些内容:

[ZIP]
EngineVersion=0
DATVersion=5127
FileName=dat-5127.zip
FilePath=/pub/antivirus/datfiles/4.x/
FileSize=13481555
Checksum=6037,021E
MD5=aaeb519d3f276b810d46642d782d8921

那就可以通过下面这些代码得到MD5的值,简单吧

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import ConfigParser
config = ConfigParser.ConfigParser()
config.readfp(open('update.ini'))
a = config.get("ZIP","MD5")
print a

写也很简单:

import ConfigParser
config = ConfigParser.ConfigParser()
# set a number of parameters
config.add_section("book")
config.set("book", "title", "the python standard library")
config.set("book", "author", "fredrik lundh")
config.add_section("ematter")
config.set("ematter", "pages", 250)
# write to file
config.write(open('1.ini', "w"))

修改也不难(添加内容):

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import ConfigParser
config = ConfigParser.ConfigParser()
config.read('1.ini')
a = config.add_section("md5")
config.set("md5", "value", "1234")
config.write(open('1.ini', "r+")) #可以把r+改成其他方式,看看结果:)

修改内容:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import ConfigParser
config = ConfigParser.ConfigParser()
config.read('1.ini')
config.set("md5", "value", "kingsoft") #这样md5就从1234变成kingsoft了
config.write(open('1.ini', "r+"))

删除部分就懒得写了,感兴趣的自己看文档:

remove_option( section, option)
Remove the specified option from the specified section. If the section does not exist, raise NoSectionError. If the option existed to be removed, return True; otherwise return False. New in version 1.6.
remove_section( section)
Remove the specified section from the configuration. If the section in fact existed, return True. Otherwise return False.

希望本文所述对大家的Python程序设计有所帮助。

(0)

相关推荐

  • python读写文件操作示例程序

    文件操作示例 复制代码 代码如下: #输入文件f = open(r'D:\Python27\pro\123.bak') #输出文件fw = open(r'D:\Python27\pro\123e.bak','w')#按行读出所有文本lines = f.readlines()num = -1for line in lines:    str = '@SES/%i/' %num    line = line.replace('@SES/1/',str)    num = num + 1    #写入

  • Python读写文件方法总结

    本文实例总结了Python读写文件方法.分享给大家供大家参考.具体分析如下: 1.open 使用open打开文件后一定要记得调用文件对象的close()方法.比如可以用try/finally语句来确保最后能关闭文件. file_object = open('thefile.txt') try: all_the_text = file_object.read( ) finally: file_object.close( ) 注:不能把open语句放在try块里,因为当打开文件出现异常时,文件对象f

  • python读写二进制文件的方法

    本文实例讲述了python读写二进制文件的方法.分享给大家供大家参考.具体如下: 初学python,现在要读一个二进制文件,查找doc只发现 file提供了一个read和write函数,而且读写的都是字符串,如果只是读写char等一个字节的还行,要想读写如int,double等多字节数 据就不方便了.在网上查到一篇贴子,使用struct模块里面的pack和unpack函数进行读写.下面就自己写代码验证一下. >>> from struct import * >>> fi

  • Python读写Excel文件的实例

    最近由于经常要用到Excel,需要根据Excel表格中的内容对一些apk进行处理,手动处理很麻烦,于是决定写脚本来处理.首先贴出网上找来的读写Excel的脚本. 1.读取Excel(需要安装xlrd): #-*- coding: utf8 -*- import xlrd fname = "reflect.xls" bk = xlrd.open_workbook(fname) shxrange = range(bk.nsheets) try: sh = bk.sheet_by_name(

  • python读写ini文件示例(python读写文件)

    很类似java的properties文件 xml文件 复制代码 代码如下: db_config.ini[baseconf]host=127.0.0.1port=3306user=rootpassword=rootdb_name=evaluting_sys[concurrent]processor=20 对应的python代码 复制代码 代码如下: #!/usr/bin/python# -*- coding:utf-8 -*-#author: lingyue.wkl#desc: use to db

  • Python读写Excel文件方法介绍

    一.读取excel 这里介绍一个不错的包xlrs,可以工作在任何平台.这也就意味着你可以在Linux下读取Excel文件. 首先,打开workbook: 复制代码 代码如下: import xlrd wb = xlrd.open_workbook('myworkbook.xls') 检查表单名字: 复制代码 代码如下: wb.sheet_names() 得到第一张表单,两种方式:索引和名字 复制代码 代码如下: sh = wb.sheet_by_index(0) sh = wb.sheet_by

  • Python读写配置文件的方法

    本文实例讲述了Python读写配置文件的方法.分享给大家供大家参考.具体分析如下: python 读写配置文件在实际应用中具有十分强大的功能,在实际的操作中也有相当简捷的操作方案,以下的文章就是对python 读写配置文件的具体方案的介绍,相信对大家学习Python有所帮助. python 读写配置文件ConfigParser模块是python自带的读取配置文件的模块.通过他可以方便的读取配置文件. 这里就来简单介绍一下python 读写配置文件的方法. 配置文件.顾名思议就是存放配置信息的文件

  • Python 文件读写操作实例详解

    一.python中对文件.文件夹操作时经常用到的os模块和shutil模块常用方法.1.得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd()2.返回指定目录下的所有文件和目录名:os.listdir()3.函数用来删除一个文件:os.remove()4.删除多个目录:os.removedirs(r"c:\python")5.检验给出的路径是否是一个文件:os.path.isfile()6.检验给出的路径是否是一个目录:os.path.isdir()7.判断是

  • python读写ini配置文件方法实例分析

    本文实例讲述了python读写ini配置文件方法.分享给大家供大家参考.具体实现方法如下: import ConfigParser import os class ReadWriteConfFile: currentDir=os.path.dirname(__file__) filepath=currentDir+os.path.sep+"inetMsgConfigure.ini" @staticmethod def getConfigParser(): cf=ConfigParser

  • Python编程中的文件读写及相关的文件对象方法讲解

    python文件读写 python 进行文件读写的内建函数是open或file file_hander(文件句柄或者叫做对象)= open(filename,mode) mode: 模式    说明 r        只读 r+      读写 w       写入,先删除源文件,在重新写入,如果文件没有则创建 w+     读写,先删除源文件,在重新写入,如果文件没有则创建(可以写入写出) 读文件: >>> fo = open("/root/a.txt") >

随机推荐