python中configparser函数_python基础14 ---函数模块4(configparser模块)

configparser模块

一、configparser模块

1、什么是configparser模块:configparser模块操作配置文件,配置文件的格式与windows ini和linux的cf文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键=值),其配置文件(INI文件)由节(section)、键、值组成。

2、configparser模块简介。

ConfigParser 是用来读取配置文件的包。配置文件的格式如下:中括号“[ ]”内包含的为section。section 下面为类似于key-value 的配置内容。

3、如何生成一个configparser配置文件。

1.首先import ConfigParser来调用ConfigParser模块 #import configparser

2.创建一个ConfigParser配置文件对象。#config = configparser.ConfigParser()

3.往配置文件中添加内容。#config["DEFAULT"] = {'ServerAliveInterval': '45', 'Compression': 'yes' }  ; config['bitbucket.org'] = {'User':'hg'}

4.打开或创建一个配置文件,然后将配置文件对象内的东西添加到配置文件中。

with open('example.ini', 'w') as configfile:

config.write(configfile)

5.读取该配置文件。#config.read('配置文件名称')

6.使用configparser模块解析配置文件时,发现的问题:

参数名称的大写全部会转换为小写。

参数名称不能含有[,]

如果含有多个名字相同的section时,会以最后一个section为准

7.读取配置文件和写入配置文件。

-read(filename)  直接读取ini文件内容

-sections() 得到所有的section,并以列表的形式返回

-options(section) 得到该section的所有option

-items(section) 得到该section的所有键值对

-get(section,option) 得到section中option的值,返回为string类型

-getint(section,option) 得到section中option的值,返回为int类型

-add_section(section) 添加一个新的section

-set( section, option, value) 对section中的option进行设置

8.案例测试

详见:http://blog.csdn.net/gexiaobaohelloworld/article/details/7976944

二、subprocess模块

1、subprocess模块的由来。

当我们需要调用系统的命令的时候,最先考虑的os模块。用os.system()和os.popen()来进行操作。但是这两个命令过于简单,不能完成一些复杂的操作,如给运行的命令提供输入或者读取命令的输出,判断该命令的运行状态,管理多个命令的并行等等。这时subprocess中的Popen命令就能有效的完成我们需要的操作。

2.subprocess.Popen命令。

subprocess.Popen('dir',shell=True) #返回的结果是个实例对象。

3、案例测试

#详细内容http://blog.csdn.net/songfreeman/article/details/50735045

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值