9、configparser模块

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

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

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

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

import configparserconfig=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 configparserconfig = 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) # Trueprint(config['bitbucket.org']["user"])  # hgprint(config['DEFAULT']['Compression']) #yesprint(config['topsecret.server.com']['ForwardX11'])  #noprint(config['bitbucket.org'])          #<Section: bitbucket.org>for key in config['bitbucket.org']:     # 注意,有default会默认default的键print(key)
>>userserveraliveintervalcompressioncompressionlevelforwardx11print(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 configparserconfig = 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 subprocessa = 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

转载于:https://www.cnblogs.com/hedeyong/p/7086025.html

Python基础(14)_python模块之configparser模块、suprocess相关推荐

  1. python基础总结:1.7、模块

    python基础总结:1.7.模块 文章目录 python基础总结:1.7.模块 1. 更多有关模块的信息 1.1 以脚本的方式执行模块 1.2 模块搜索路径 1.3 "编译过的" ...

  2. 视频教程-快速入门Python基础教程_Python基础知识大全-Python

    快速入门Python基础教程_Python基础知识大全 十余年计算机技术领域从业经验,在中国电信.盛大游戏等多家五百强企业任职技术开发指导顾问,国内IT技术发展奠基人之一. 杨千锋 ¥99.00 立即 ...

  3. 视频教程-快速入门Python基础教程_Python基础进阶视频-Python

    快速入门Python基础教程_Python基础进阶视频 十余年计算机技术领域从业经验,在中国电信.盛大游戏等多家五百强企业任职技术开发指导顾问,国内IT技术发展奠基人之一. 杨千锋 ¥199.00 立 ...

  4. python中configparser详解_Python中的ConfigParser模块使用详解

    1.基本的读取配置文件 -read(filename) 直接读取ini文件内容 -sections() 得到所有的section,并以列表的形式返回 -options(section) 得到该sect ...

  5. python配置文件解析_Python中配置文件解析模块-ConfigParser

    Python中有ConfigParser类,可以很方便的从配置文件中读取数据(如DB的配置,路径的配置). 配置文件的格式是: []包含的叫section, section 下有option=valu ...

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

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

  7. Python常用模块之configparser模块

    该模块适用于配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键=值). 创建文件: [DEFAULT] ServerAliveInterv ...

  8. Python基础笔记_Day04_数据类型、math模块、random模块、string模块

    Day04_数据类型.math模块.random模块.string模块 04.01_Python语言基础(Python中的数据类型)(了解) 04.02_Python语言基础(Num数据类型)(掌握) ...

  9. Python基础教程:线程操作(oncurrent模块)详解

    进程是cpu资源分配的最小单元,一个进程中可以有多个线程. 线程是cpu计算的最小单元. 对于Python来说他的进程和线程和其他语言有差异,是有GIL锁. GIL锁 GIL锁保证一个进程中同一时刻只 ...

最新文章

  1. python3 collections模块_Python3之内建模块collections
  2. Failed to find data source: text
  3. C语言实用算法系列之DOS传参“加减乘除计算器”
  4. Fedora 20 配置
  5. ansible获取linux信息,ansible 获取系统信息的一些范例,ansible系统信息
  6. API不是从业务抽象出来的(1):设计思维
  7. 微软应用商店错误代码“0x80131500”怎么修复?
  8. 关于如何卸载VS2012
  9. java编写蠕虫病毒_教大家编写蠕虫病毒
  10. 电影推广思路详解,最权威的电影推广方案
  11. 同事离职做假证,顺利拿到大公司offer,15k一下子到了24k
  12. 阿里云服务器搭建和宝塔面板连接
  13. 当其为质数返回true,否则返回false
  14. ArcGIS Pro添加在线底图
  15. 图片(旋转/缩放/翻转)变换效果(ccs3/滤镜/canvas)
  16. 新的 ES2022 规范终于发布了,我总结了8个实用的新功能
  17. nginx降权+安装php
  18. 30天学会javase
  19. iOS开发者账号的申请
  20. VGA显示模式及相关参数

热门文章

  1. 电脑桌面的东西突然不显示了
  2. 干货 | 万字长文全面解析GraphQL,携程微服务背景下的前后端数据交互方案
  3. Spring@Cacheable注解在类内部调用失效的问题
  4. 2018年11月份GitHub上最热门的开源项目
  5. 预见未来 | 数据智能的现在与未来
  6. Java的时间为何从1970年1月1日开始
  7. 当git上只做文件大小写重命名的修改时,如何躲坑...
  8. Flowable节点跳转
  9. MySQL: load data infile 需要注意的点
  10. 框架:AspectJ