Dzwebs.Net

撰写电脑技术杂文十余年

python中的configparser模块读取Config.conf配置文件的例子

Admin | 2023-8-16 19:19:09 | 被阅次数 | 945

温馨提示!

如果未能解决您的问题,请点击搜索;登陆可复制文章,点击登陆

  如题所示,下面的代码功能是通过configparser模块读取Config.conf配置文件中的节点及节点下的键及值的方法。

  一、代码如下

  import configparser

  MyConf=configparser.ConfigParser()

  MyConf.read('Config.conf')

  print(MyConf.sections())

  for node in MyConf.sections():#eachnode
      for key in MyConf.options(node):#Key of eachnode
          print(node,key,MyConf.get(node,key))

  二、配置文件Config.conf如下

  [host]
  host=192.168.10.41
  port=21

  #共享路径
  [ftppath]
  sharepath=d:\\ftp\\sharefiles

  #用户1的帐户及路径
  [user1]
  user1name=admin1
  user1pwd=admin1
  user1path=d:\\ftp

  #用户2的帐户及路径
  [user2]
  user2name=admin2
  user2pwd=admin2
  user2path=d:\\ftp

  三、运行输出

  ['host', 'ftppath', 'user1', 'user2']
  host host 192.168.10.41
  host port 21
  ftppath sharepath d:\\ftp\\sharefiles
  user1 user1name admin1
  user1 user1pwd admin1
  user1 user1path d:\\ftp
  user2 user2name admin2
  user2 user2pwd admin2
  user2 user2path d:\\ftp


该杂文来自: 最新技术

上一篇:python使用pyftpdlib库作为FTP服务器出现乱码的解

下一篇:rust通过Cell单独修改struct中的某项而不是全部

网站备案号:

网站备案号:滇ICP备11001339号-7

版权属性:

Copyright 2007-2021-forever Inc. all Rights Reserved.

联系方式:

Email:dzwebs@126.com QQ:83539231 访问统计