欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2022-04-03 14:37:10
...
很类似java的properties文件

xml文件

复制代码 代码如下:

db_config.ini
[baseconf]
host=127.0.0.1
port=3306
user=root
password=root
db_name=evaluting_sys
[concurrent]
processor=20

对应的python代码

复制代码 代码如下:

#!/usr/bin/python
# -*- coding:utf-8 -*-
#author: lingyue.wkl
#desc: use to db ops
#---------------------
#2012-02-18 created

#---------------------
import sys,os
import ConfigParser

class Db_Connector:
def __init__(self, config_file_path):
cf = ConfigParser.ConfigParser()
cf.read(config_file_path)

s = cf.sections()
print 'section:', s

o = cf.options("baseconf")
print 'options:', o

v = cf.items("baseconf")
print 'db:', v

db_host = cf.get("baseconf", "host")
db_port = cf.getint("baseconf", "port")
db_user = cf.get("baseconf", "user")
db_pwd = cf.get("baseconf", "password")

print db_host, db_port, db_user, db_pwd

cf.set("baseconf", "db_pass", "123456")
cf.write(open("config_file_path", "w"))
if __name__ == "__main__":
f = Db_Connector("../conf/db_config.ini")

得到结果:

复制代码 代码如下:

section: ['concurrent', 'baseconf']
options: ['host', 'db_name', 'user', 'password', 'port']
db: [('host', '127.0.0.1'), ('db_name', 'evaluting_sys'), ('user', 'root'), ('password', 'root'), ('port', '3306')]
127.0.0.1 3306 root root

通用模块:支持命令行+import两种形式
ini_op.py

复制代码 代码如下:

#!/usr/bin/python
# -*- coding:utf-8 -*-
#author: lingyue.wkl
#desc: use to read ini
#---------------------
#2012-02-18 created
#2012-09-02 changed for class support

#---------------------
import sys,os,time
import ConfigParser


class Config:
def __init__(self, path):
self.path = path
self.cf = ConfigParser.ConfigParser()
self.cf.read(self.path)
def get(self, field, key):
result = ""
try:
result = self.cf.get(field, key)
except:
result = ""
return result
def set(self, filed, key, value):
try:
self.cf.set(field, key, value)
cf.write(open(self.path,'w'))
except:
return False
return True
def read_config(config_file_path, field, key):
cf = ConfigParser.ConfigParser()
try:
cf.read(config_file_path)
result = cf.get(field, key)
except:
sys.exit(1)
return result

def write_config(config_file_path, field, key, value):
cf = ConfigParser.ConfigParser()
try:
cf.read(config_file_path)
cf.set(field, key, value)
cf.write(open(config_file_path,'w'))
except:
sys.exit(1)
return True

if __name__ == "__main__":
if len(sys.argv) sys.exit(1)

config_file_path = sys.argv[1]
field = sys.argv[2]
key = sys.argv[3]
if len(sys.argv) == 4:
print read_config(config_file_path, field, key)
else:
value = sys.argv[4]
write_config(config_file_path, field, key, value)

第二个示例


复制代码 代码如下:

import os
import ConfigParser

def main():
cp = ConfigParser.ConfigParser()
cf = open(u"in.ini")
cp.readfp(cf)

secs = cp.sections()
print cp.sections()

for sec in secs:
opts = cp.options(sec)
for opt in opts:
val = cp.get(sec, opt)
val += "test....."
cp.set(sec, opt, val)

cp.write(open("out.ini", "w"))

if __name__ == '__main__':
main()

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

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

相关文章

相关视频


网友评论

文明上网理性发言,请遵守 新闻评论服务协议

我要评论
  • python读写ini文件示例(python读写文件)
  • 专题推荐

    作者信息
    python读写ini文件示例(python读写文件)

    认证0级讲师

    推荐视频教程
  • python读写ini文件示例(python读写文件)javascript初级视频教程
  • python读写ini文件示例(python读写文件)jquery 基础视频教程
  • 视频教程分类