如何用Python读取一个配置文件?首先你要了解配置文件里面有哪些字段,你要读的信息分布在哪里。
Python读配置文件用到的模块有:ConfigParser。需要把该模块import一下。
下面的例子是在配置文件中读取几个值:分别是日志路径,用户名,密码和ip。
<span style="font-size:18px;">myPath = "日志文件所在的目录"
cf = ConfigParser.ConfigParser()
cf.read("es_stat.conf")
logpath = cf.get("Log Path","path")
user = cf.get("Server","user")
ip = cf.get("Server","ip")
psw = cf.get("Server","password")
</span>