欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python config文件的讀寫操作示例

 更新時間:2019年09月27日 11:14:58   作者:以夢為馬越騎越傻  
這篇文章主要介紹了python config文件的讀寫操作,結(jié)合簡單示例形式分析了Python針對config文件的設置、讀取、寫入相關(guān)操作技巧,需要的朋友可以參考下

本文實例講述了python config文件的讀寫操作。分享給大家供大家參考,具體如下:

1、設置配置文件

[mysql]
host = 1234
port = 3306
user = root
password = Zhsy08241128
database = leartd

2、讀取配置文件

import configparser
import os
conf= configparser.ConfigParser()
def readConf():
  '''讀取配置文件'''
  root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  conf.read(root_path + '/ceshi/conf/app.conf') # 文件路徑
  print(conf)
  name = conf.get("mysql", "host") # 獲取指定section 的option值
  print(name)

3、寫入配置文件

def writeConf():
  '''寫入配置文件'''
  root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  conf.read(root_path + '/ceshi/conf/app.conf') # 文件路徑
  conf.set("mysql", "host", "1234") # 修改指定section 的option
  conf.write(open(root_path + '/ceshi/conf/app.conf', 'w'))

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python函數(shù)使用技巧總結(jié)》、《Python面向?qū)ο蟪绦蛟O計入門與進階教程》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總

希望本文所述對大家Python程序設計有所幫助。

相關(guān)文章

最新評論