python讀寫ini文件示例(python讀寫文件)
很類似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
對應(yīng)的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")
得到結(jié)果:
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) < 4:
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)
第二個(gè)示例
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 3.74 運(yùn)行import numpy as np 報(bào)錯(cuò)lib\site-packages\numpy\__init__.py
- Python使用minidom讀寫xml的方法
- Python讀寫ini文件的方法
- Python操作配置文件ini的三種方法講解
- Python中使用ConfigParser解析ini配置文件實(shí)例
- Python讀取配置文件(config.ini)以及寫入配置文件
- Python讀取ini文件、操作mysql、發(fā)送郵件實(shí)例
- python構(gòu)造函數(shù)init實(shí)例方法解析
- 使用python腳本自動創(chuàng)建pip.ini配置文件代碼實(shí)例
- python對配置文件.ini進(jìn)行增刪改查操作的方法示例
- Python 使用ConfigParser操作ini配置文件
相關(guān)文章
通過python爬蟲mechanize庫爬取本機(jī)ip地址的方法
python中的mechanize算是一個(gè)比較古老的庫了,在python2的時(shí)代中,使用的多一些,在python3以后就很少使用了,現(xiàn)在已經(jīng)是2202年了,可能很多人都沒聽說過mechanize,這不要緊,我們先來簡單的講解一下,如何使用mechanize,感興趣的朋友一起看看吧2022-08-08Python rabbitMQ如何實(shí)現(xiàn)生產(chǎn)消費(fèi)者模式
這篇文章主要介紹了Python rabbitMQ如何實(shí)現(xiàn)生產(chǎn)消費(fèi)者模式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08Python 實(shí)用技巧之利用Shell通配符做字符串匹配
這篇文章主要介紹了Python 實(shí)用技巧之利用Shell通配符做字符串匹配的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08Python漏洞驗(yàn)證程序Poc利用入門到實(shí)戰(zhàn)編寫
這篇文章主要為大家介紹了Python?Poc利用入門到實(shí)戰(zhàn)編寫實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02Python Numpy學(xué)習(xí)之索引及切片的使用方法
數(shù)組中的元素可以通過索引以及切片的手段進(jìn)行訪問或者修改,和列表的切片操作一樣。本文將詳細(xì)為大家介紹一下Python中的科學(xué)計(jì)算庫-Numpy的索引及切片的使用方法2022-01-01Django restful framework生成API文檔過程詳解
這篇文章主要介紹了Django restful framework生成API文檔過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11python基于paramiko庫遠(yuǎn)程執(zhí)行 SSH 命令,實(shí)現(xiàn) sftp 下載文件
這篇文章主要介紹了python基于paramiko庫遠(yuǎn)程執(zhí)行 SSH 命令,實(shí)現(xiàn) sftp 下載文件的方法,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-03-03