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. Android JNI和NDK学习(06)--JNI的数据类型(转)

    本文转自:http://www.cnblogs.com/skywang12345/archive/2013/05/23/3094037.html 本文介绍JNI的数据类型.NDK中关于JNI数据类型的 ...

  2. python装饰器的理解

    学习python,发现装饰器是一个比较难理解的地方. 下面用代码来说明. 装饰器的作用是为了切面编程(AOP).这种编程在java上有很多实现方式.下面直接说明吧: 1.作为装饰器的函数至少有两个de ...

  3. jquery插件实现分页

    Query Pagination分页插件 原项目地址:http://plugins.jquery.com/project/pagination 版本:v1.2 源文件下载:英文原版 或 中文翻译修改版 ...

  4. linux IP动态变动之后 , 需要做的杂项操作

    linux的动态ip经常变来变去,目前还没找到固定它不变化的方法.所以每次变动之后都需要做以下的操作,极其麻烦.(必须找到让linux IP 固定的方法) 1.先找到变化之后的动态ip地址 ifcon ...

  5. 从获取点击事件根元素谈 target和currentTarget

    事情由来: 写了一个点击事件,想获取根元素,想的直接用current就行了,因为之前就是这么用的,但是之前的点击元素是没子元素的,current就是根元素,但是这次点击元素内部有子元素,current ...

  6. 1. 写出一个能创建多级目录的 PHP 函数(新浪网技术部)

    function create_dir($path,$mode){ if (is_dir($path)){ echo "该目录已经存在"; }else{ if(mkdir($pat ...

  7. urllib基本使用 urlopen(),Request

    urllib包含的常用模块:import urllib.request # 打开和读取url请求import urllib.error # 异常处理模块import urllib.parse # ur ...

  8. NewtonSoft.Json NULL转空字符串

    from:http://www.cnblogs.com/hetuan/articles/4565702.html NewtonSoft.Json对需要转为JSON字符串的对象的NULL值以及DBNul ...

  9. linux之shell之if、while、for语句介绍

    一.基本判断条件 1)逻辑运算符 -a    expr1 -a expr2    逻辑与 -o    expr1 -o expr2    逻辑或 !     !expr1                ...

  10. [Spring MVC]学习笔记--基础Servlet

    Servlet是一个用Java编写的应用程序,在服务器上运行,处理请求的信息并将其发送到客户端. Servlet的客户端提出请求并获得该请求的响应. 对于所有的客户端请求,只需要创建Servlet的实 ...