首页 > 编程语言 > 详细

【python】读取cfg配置文件

时间:2021-09-06 21:21:33      阅读:21      评论:0      收藏:0      [点我收藏+]
config.cfg文件内容:

技术分享图片

 

 现需读取config.cfg里的host内容:

 1 # @Time    : 2021/9/4 20:21
 3 # @File    : readCfg.py
 4 import os
 5 
 6 from configparser import ConfigParser
 7 import os
 8 
 9 class ReadCfg():
10     def __init__(self):
11         pass
12 
13     def getPath(self):
14         #获取当前目录
15         current_dir = os.path.abspath(‘‘)
16         #获取上一级目录
17         filePath = os.path.dirname(current_dir)
18         #再获取上一级目录
19         lastFilePath = os.path.dirname(filePath)
20         #拼接配置文件的路径
21         cfgPath =os.path.join(lastFilePath,conf,config.cfg)
22         print(cfgPath)
23         return cfgPath
24     #默认获取主机的地址
25     def readCfg(self,section=hostname,option = host):
26         #拼接配置文件的路径
27         con = ConfigParser()
28         con.read(self.getPath(),utf-8)
29         res = con.get(section,option)
30         return res
31 
32 if __name__ == __main__:
33     readCfg = ReadCfg()

 

 



【python】读取cfg配置文件

原文:https://www.cnblogs.com/chooperman/p/15233142.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!