9、configparser模块

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

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

  1. [DEFAULT]
  2. ServerAliveInterval =
  3. Compression = yes
  4. CompressionLevel =
  5. ForwardX11 = yes
  6.  
  7. [bitbucket.org]
  8. User = hg
  9.  
  10. [topsecret.server.com]
  11. Port =
  12. ForwardX11 = no

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

  1. import configparser
  2.  
  3. config=configparser.ConfigParser()
  4.  
  5. config['DEFAULT'] ={
  6. 'ServerAliveInterval': '45',
  7. 'Compression': 'yes',
  8. 'CompressionLevel': '9',
  9. 'ForwardX11':'yes'
  10. }
  11. config['bitbucket'] = {'User':'hg'}
  12. config['topsecret.server.com'] = {'Host Port':'50022','ForwardX11':'no'}
  13. with open('config.ini','w',encoding='utf-8') as f:
  14. config.write(f)  

查找文件:以字典的方式

  1. import configparser
  2.  
  3. config = configparser.ConfigParser()
  4.  
  5. #---------------------------查找文件内容,基于字典的形式
  6.  
  7. print(config.sections()) # []
  8.  
  9. config.read('config.ini') #读取文件
  10.  
  11. print(config.sections()) # ['bitbucket.org', 'topsecret.server.com']
  12.  
  13. print('bytebong.com' in config) # False
  14. print('bitbucket.org' in config) # True
  15.  
  16. print(config['bitbucket.org']["user"]) # hg
  17.  
  18. print(config['DEFAULT']['Compression']) #yes
  19.  
  20. print(config['topsecret.server.com']['ForwardX11']) #no
  21.  
  22. print(config['bitbucket.org']) #<Section: bitbucket.org>
  23.  
  24. for key in config['bitbucket.org']: # 注意,有default会默认default的键
  25. print(key)
  26. >>user
  27. serveraliveinterval
  28. compression
  29. compressionlevel
  30. forwardx11
  31.  
  32. print(config.options('bitbucket.org')) # 同for循环,找到'bitbucket.org'下所有键
  33. >>['user', 'serveraliveinterval', 'compression', 'compressionlevel', 'forwardx11']
  34.  
  35. print(config.items('bitbucket.org')) #找到'bitbucket.org'下所有键值对
  36. >>[('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('forwardx11', 'yes'), ('user', 'hg')]

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

增删改操作

  1. import configparser
  2.  
  3. config = configparser.ConfigParser()
  4. config.read('example.ini')
  5.  
  6. #增加
  7. config.add_section('yuan')
  8. #删除
  9. config.remove_section('bitbucket.org')
  10. config.remove_option('topsecret.server.com',"forwardx11")
  11. #修改
  12. config.set('topsecret.server.com','k1','11111')
  13. config.set('yuan','k2','22222')
  14.  
  15. 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参数。表示与子进程通信的标准流。

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

subprocess.PIPE

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

  1. import subprocess
  2. s=subprocess.Popen('dir',shell=True,stdout=subprocess.PIPE)
  3. 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. C++语言基础(13)-抽象类和纯虚函数

    一.基本语法 在C++中,可以将虚函数声明为纯虚函数,语法格式为: ; 纯虚函数没有函数体,只有函数声明,在虚函数声明的结尾加上=0,表明此函数为纯虚函数. 最后的=0并不表示函数返回值为0,它只起形 ...

  2. spark-streaming的checkpoint机制源码分析

    转发请注明原创地址 http://www.cnblogs.com/dongxiao-yang/p/7994357.html spark-streaming定时对 DStreamGraph 和 JobS ...

  3. hdu 2217 Visit

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2217 题目解释:起始位置在原点,给你固定的时间,让你左右跑,求在规定的时间内你最多能跑多少个点: 解决 ...

  4. iOS开发中邮箱,电话号码,身份证,密码,昵称正则表达式验证

    //邮箱 + (BOOL) validateEmail:(NSString *)email {     NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@ ...

  5. Map根据value排序

    网上找到的资源, package com.test.ch1; import java.util.ArrayList; import java.util.Collections; import java ...

  6. 使用 NGUI Toggle 制作单选框

    好久没写了,今天来把关于NGUI的做的简单功能发上来~ 这个是做单选框的.用了新版本的NGUI后,发现没有以前的Checkbox了,在网上查了之后才知道是用Toggle代替了以前的Checkbox.现 ...

  7. 青蛙的约会 扩展欧几里得 方程ax+by=c的整数解 一个跑道长为周长为L米,两只青蛙初始位置为x,y;(x!=y,同时逆时针运动,每一次运动分别为m,n米;问第几次运动后相遇,即在同一位置。

    /** 题目:青蛙的约会 链接:https://vjudge.net/contest/154246#problem/R 题意:一个跑道长为周长为L米,两只青蛙初始位置为x,y:(x!=y,同时逆时针运 ...

  8. 在linux下使用curl

    使用curl从 ftp下载文件 curl ftp://192.168.31.164/lrzsz-0.12.20.tar.gz --user root:123456 -o lrzsz-0.12.20.t ...

  9. 05、(通过nat123软件) 实现用自己电脑搭建一个网站

    (通过nat123软件) 实现用自己电脑搭建一个网站 准备: Tomcat:这个是web容器,其实有了这个就已经让电脑成为服务器了,在自己电脑上可以通过 localhost:8080/xxx 来访问自 ...

  10. 关于angularjs的复选框选中

    最近在做复选框,业务人员要求选中一块区域里的任何一个适合,复选框呈现选中状态,然而,我的复选框是ng-repeat出来的,刚开始我想用directive指令,但是不知道为什么,我一旦设置了$(this ...