如题所示,下面的代码功能是通过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