• 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

  • 相关阅读:
    springboot02-SpringSecurity
    java基础07- 二维数组
    java基础06-数组的使用
    逆向工程核心原理——第四章
    逆向工程核心原理——第三章
    Crackme_1_Acid_burn
    Crackme_2_Afkayas.1
    逆向工程核心原理——第二章
    MessageBox
    Python(基础)
  • 原文地址:https://www.cnblogs.com/xuanan/p/7085951.html
Copyright © 2020-2023  润新知