python100课程_模块_configparse

    python100课程_模块_configparse

讲解:ConfigParser模块在python3中修改为configparser.这个模块定义了一个ConfigParser类,该类的作用是使配置文件生效,配置文件的格式和windows的INI文件的格式相同。可以包含一个或多个节(section),每个节可以有多个参数(键=值)。

     该模块的作用 就是使用模块中的RawConfigParser()ConfigParser()SafeConfigParser()这三个方法(三者择其一),创建一个对象使用对象的方法对指定的配置文件做增删改查 操作。

配置文件有不同的片段组成和Linux中repo文件中的格式类似

#!usr/bin/env python
#-*- coding:utf-8 -*-

# import ConfigParser
#
# cg = ConfigParser.ConfigParser()
#
# cg.add_section('lengdong')
# cg.set('lengdong','sn','kj123456')
# cg.set('lengdong','cmd','adb -s %(sn)s push')
#
# cg.add_section('shaopian')
# cg.set('shaopian','sn','sj000000')
# cg.set('shaopian','cmd','adb -s %(sn)s load')
# f = open('example.cfg','w')
# cg.write(f)
# f.close()

####### 读
import ConfigParser

cg = ConfigParser.ConfigParser()
cg.read('example.cfg')
cmd_lengdong = cg.get('lengdong','cmd',0)
print (cmd_lengdong)
View Code

 

posted on 2018-05-18 08:59  lexn  阅读(124)  评论(0编辑  收藏  举报

导航