9、configparser模块

  模块适用于配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键=值)。

常见的软件格式文档格式如下:

 [DEFAULT]
ServerAliveInterval =
Compression = yes
CompressionLevel =
ForwardX11 = yes [bitbucket.org]
User = hg [topsecret.server.com]
Port =
ForwardX11 = no

使用configparser模块,创建格式文档:

import configparser

config=configparser.ConfigParser()

config['DEFAULT'] ={
'ServerAliveInterval': '45',
'Compression': 'yes',
'CompressionLevel': '9',
'ForwardX11':'yes'
}
config['bitbucket'] = {'User':'hg'}
config['topsecret.server.com'] = {'Host Port':'50022','ForwardX11':'no'}
with open('config.ini','w',encoding='utf-8') as f:
config.write(f)  

查找文件:以字典的方式

import configparser

config = configparser.ConfigParser()

#---------------------------查找文件内容,基于字典的形式

print(config.sections())        #  []

config.read('config.ini')     #读取文件

print(config.sections())        #   ['bitbucket.org', 'topsecret.server.com']

print('bytebong.com' in config) # False
print('bitbucket.org' in config) # True print(config['bitbucket.org']["user"]) # hg print(config['DEFAULT']['Compression']) #yes print(config['topsecret.server.com']['ForwardX11']) #no print(config['bitbucket.org']) #<Section: bitbucket.org> for key in config['bitbucket.org']: # 注意,有default会默认default的键
print(key)

>>user
serveraliveinterval
compression
compressionlevel
forwardx11 print(config.options('bitbucket.org')) # 同for循环,找到'bitbucket.org'下所有键
>>['user', 'serveraliveinterval', 'compression', 'compressionlevel', 'forwardx11'] print(config.items('bitbucket.org')) #找到'bitbucket.org'下所有键值对
>>[('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('forwardx11', 'yes'), ('user', 'hg')]

print(config.get('bitbucket.org','compression')) # yes get方法取深层嵌套的值
>>yes
 

增删改操作

import configparser

config = configparser.ConfigParser()
config.read('example.ini') #增加
config.add_section('yuan')
#删除
config.remove_section('bitbucket.org')
config.remove_option('topsecret.server.com',"forwardx11")
#修改
config.set('topsecret.server.com','k1','11111')
config.set('yuan','k2','22222') config.write(open('new2.ini', "w"))  

10、subprocess模块

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

      subprocess模块允许一个进程创建一个新的子进程,通过管道连接到子进程的stdin/stdout/stderr,获取子进程的返回值等操作。 

简单命令:

a = subprocess.Popen('ls',shell=True)

在windows中,创建子进程需要添加:shell=True

在Linux中,创建子进程不需添加shell=True,在使用命令参数时需要添加shell=True

在创建Popen对象时,subprocess.PIPE可以初始化stdin, stdout或stderr参数。表示与子进程通信的标准流。

import subprocess

a = subprocess.Popen('ls',shell=True)       #  创建一个新的进程,与主进程不同步
print('>>>',a) #a是Popen的一个实例对象
>>> <subprocess.Popen object at 0x0000024BB71BB128> #in Linux 系统中
subprocess.Popen('ls -l',shell=True) #结果以字符串显示 subprocess.Popen(['ls','-l']) #结果以列表显示

subprocess.PIPE

  在创建Popen对象时,subprocess.PIPE可以初始化stdin, stdout或stderr参数。表示与子进程通信的标准流。

import  subprocess
s=subprocess.Popen('dir',shell=True,stdout=subprocess.PIPE)
print(s.stdout.read().decode('gbk'))

  subprocess创建了子进程,结果本在子进程中,if 想要执行结果转到主进程中,就得需要一个管道,即 : stdout=subprocess.PIPE

Python基础(14)_python模块之configparser模块、suprocess的更多相关文章

  1. python基础14 ---函数模块4(configparser模块)

    configparser模块 一.configparser模块 1.什么是configparser模块:configparser模块操作配置文件,配置文件的格式与windows ini和linux的c ...

  2. Python之xml文档及配置文件处理(ElementTree模块、ConfigParser模块)

    本节内容 前言 XML处理模块 ConfigParser/configparser模块 总结 一.前言 我们在<中我们描述了Python数据持久化的大体概念和基本处理方式,通过这些知识点我们已经 ...

  3. 【转】Python之xml文档及配置文件处理(ElementTree模块、ConfigParser模块)

    [转]Python之xml文档及配置文件处理(ElementTree模块.ConfigParser模块) 本节内容 前言 XML处理模块 ConfigParser/configparser模块 总结 ...

  4. python基础系列教程——Python3.x标准模块库目录

    python基础系列教程——Python3.x标准模块库目录 文本 string:通用字符串操作 re:正则表达式操作 difflib:差异计算工具 textwrap:文本填充 unicodedata ...

  5. [xml模块、hashlib模块、subprocess模块、os与sys模块、configparser模块]

    [xml模块.hashlib模块.subprocess模块.os与sys模块.configparser模块] xml模块 XML:全称 可扩展标记语言,为了能够在不同的平台间继续数据的交换,使交换的数 ...

  6. 十四. Python基础(14)--递归

    十四. Python基础(14)--递归 1 ● 递归(recursion) 概念: recursive functions-functions that call themselves either ...

  7. Python 自学基础(四)——time模块,random模块,sys模块,os模块,loggin模块,json模块,hashlib模块,configparser模块,pickle模块,正则

    时间模块 import time print(time.time()) # 当前时间戳 # time.sleep(1) # 时间延迟1秒 print(time.clock()) # CPU执行时间 p ...

  8. python基础——14(shelve/shutil/random/logging模块/标准流)

    一.标准流 1.1.标准输入流 res = sys.stdin.read(3) 可以设置读取的字节数 print(res) res = sys.stdin.readline() print(res) ...

  9. python基础-第六篇-6.2模块

    python之强大,就是因为它其提供的模块全面,模块的知识点不仅多,而且零散---一个字!错综复杂 没办法,二八原则抓重点咯!只要抓住那些以后常用开发的方法就可以了,哪些是常用的?往下看--找答案~ ...

随机推荐

  1. proxy_redirect参数的作用

    Nginx的代理功能太完善了,我们看看proxy_redirect参数的作用. 案例说明: 要做一个html.aslibra.com的域名处理很多网站的html内容,当然是后端的服务器了,目录分析 h ...

  2. TPM概述

    TPM(Trusted Platform Module)安全芯片,是指符合TPM(可信赖平台模块)标准的安全芯片.标准由TCG(可信赖计算组织,Trusted Computing Group)提出,目 ...

  3. UVA11082 Matrix Decompressing 最大流建模解矩阵,经典

    /** 题目:UVA11082 Matrix Decompressing 链接:https://vjudge.net/problem/UVA-11082 题意:lrj入门经典P374 已知一个矩阵的行 ...

  4. A - Bi-shoe and Phi-shoe 欧拉函数

    /** 题目:A - Bi-shoe and Phi-shoe 链接:https://vjudge.net/contest/154246#problem/A 题意:每一个数都有一个得分,它的得分就是, ...

  5. Python Theano 一键安装

    Download Anaconda Anaconda is a completely free Python distribution (including for commercial use an ...

  6. linux oracle配置开机启动

    参考:http://jingyan.baidu.com/article/b2c186c8fe4306c46ef6ff16.html 先以root身份登录到linux系统, 1. 修改vi /etc/o ...

  7. java 证书 .cer 和 .pfx

    作为文件形式存在的证书一般有这几种格式: 1.带有私钥的证书 由Public Key Cryptography Standards #12,PKCS#12标准定义,包含了公钥和私钥的二进制格式的证书形 ...

  8. hdu 4005(边双连通)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4005 思路:首先考虑边双连通分量,如果我们将双连通分量中的边删除,显然我们无法得到非连通图,因此要缩点 ...

  9. 2204 Problem A(水)

    问题 A: [高精度]被限制的加法 时间限制: 1 Sec  内存限制: 16 MB 提交: 54  解决: 29 [提交][状态][讨论版] 题目描述 据关押修罗王和邪狼监狱的典狱长吹嘘,该监狱自一 ...

  10. mybatis基础,mybatis配置文件核心组件typeAliases元素

    typeAliases元素,术语类型别名 类型别名是为 Java 类型设置一个短的名字.它只和 XML 配置有关,存在的意义仅在于用来减少类完全限定名的冗余 <typeAliases> & ...