數據源處理1-configparser讀取.ini配置文件

 

import os
import configparser
current_path =os.path.dirname(__file__)#獲取config當前文件路徑
config_file_path = os.path.join(current_path,'..','conf','localconfig.ini')#獲取配置文件的路徑

class ConfigUtils: #類封裝、駝峯式命名法
def __init__(self,cfg_path=config_file_path):
self.cfg =configparser.ConfigParser()#創建一個配置文件對象
self.cfg.read(cfg_path) #創建好後去讀取cfg_path

def HOSTS(self):
hosts_value = self.cfg.get('default','hosts') #獲取節和key
return hosts_value

local_config = ConfigUtils() #創建對象,測試代碼
if __name__=='__main__':
print(local_config.HOSTS())

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章