常用模块-----configparser subprocess
configparser 模块
功能:操作模块类的文件,configparser类型文件的操作类似于字典,大多数用法和字典相同。
新建文件:
import configparser cfg=configparser.ConfigParser() cfg['DEFAULT']={'ServerAliveInterval': '45','Compression': 'yes','CompressionLevel': '9','ForwardX11':'yes'} cfg['bitbucket.org']={'User':'hg'} cfg['topsecret.server.com']={'host port':'50022','Forwardx11':'no'} with open('cfg.int','w') as f:cfg.write(f) #DEFAULT 关键字,是默认参数,将DEFAULT 里的内容(value)匹配给每个自定义模块,比如:bitbucket.org
cfg.int 文件内容如下:
[DEFAULT]serveraliveinterval = 45compression = yescompressionlevel = 9forwardx11 = yes [bitbucket.org]user = hg [topsecret.server.com]host port = 50022forwardx11 = no 操作文件内容
import configparserconfig = configparser.ConfigParser()#---------------------------查找文件内容,基于字典的形式print(config.sections()) # [] config.read('example.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)print(config.options('bitbucket.org')) # 同for循环,找到'bitbucket.org'下所有键print(config.items('bitbucket.org')) #找到'bitbucket.org'下所有键值对print(config.get('bitbucket.org','compression')) # yes get方法取深层嵌套的值
增删改操作
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"))
subprocess模块
subprocess模块允许一个进程创建一个新的子进程,通过管道连接到子进程的stdin/stdout/stderr,获取子进程的返回值等操作。
import subprocess# 创建一个新的进程,与主进程不同步 if in win: s=subprocess.Popen('dir',shell=True) s=subprocess.Popen('ls') s.wait() # s是Popen的一个实例对象print('ending...')
子进程文本控制流
可以在Popen()建立子进程的时候改变标准输入、标准输出和标准错误,并可以利用subprocess.PIPE将多个子进程的输入和输出连接在一起,构成管道(pipe):
import subprocess# s1 = subprocess.Popen(["ls","-l"], stdout=subprocess.PIPE) # print(s1.stdout.read())#s2.communicate() s1 = subprocess.Popen(["cat","/etc/passwd"], stdout=subprocess.PIPE) s2 = subprocess.Popen(["grep","0:0"],stdin=s1.stdout, stdout=subprocess.PIPE) out = s2.communicate()print(out)
转载于:https://www.cnblogs.com/mona524/p/7086677.html
常用模块-----configparser subprocess相关推荐
- Python常用模块之subprocess模块
当我们需要调用系统的命令的时候,最先考虑的os模块.用os.system()和os.popen()来进行操作. 但是这两个命令过于简单,不能完成一些复杂的操作,如给运行的命令提供输入或者读取命令的输出 ...
- python常用模块-调用系统命令模块(subprocess)
python常用模块-调用系统命令模块(subprocess) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. subproces基本上就是为了取代os.system和os.spaw ...
- 常用模块之hashlib,configparser,logging模块
常用模块二 hashlib模块 hashlib提供了常见的摘要算法,如md5和sha1等等. 那么什么是摘要算法呢?摘要算法又称为哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度固定 ...
- python常用模块:re模块案例、subprocess
今日内容: 一.re模块 二.re练习 三.subproces模块 一.re模块 import re# [] 范围匹配 中间 用-来连接 # re.findall("[a-zA-Z0-9]& ...
- Python常用模块——目录
Python常用模块学习 Python模块和包 Python常用模块time & datetime &random 模块 Python常用模块os & sys & sh ...
- Day05 - Python 常用模块
1. 模块简介 模块就是一个保存了 Python 代码的文件.模块能定义函数,类和变量.模块里也能包含可执行的代码. 模块也是 Python 对象,具有随机的名字属性用来绑定或引用. 下例是个简单的模 ...
- python全栈开发中级班全程笔记(第二模块、第四章)(常用模块导入)
python全栈开发笔记第二模块 第四章 :常用模块(第二部分) 一.os 模块的 详解 1.os.getcwd() :得到当前工作目录,即当前python解释器所在目录路径 impor ...
- python最常用的编程方式是什么_python常用模块和对象编程
1.摘要 常用模块 对象编程 领域模型 2.常用模块 2.1shutil模块 2.1.1 shutil.copyfileobj(fsrc, fdst[, length]) 1 f = open(&qu ...
- Python自学——python的常用模块
Python学习--python的常用模块 原文作者:佛山小程序员 原文链接:https://blog.csdn.net/weixin_44192923/article/details/8656325 ...
最新文章
- 超分辨率(super-resolution)VS解模糊(deblur)
- 《庆余年》里范闲背了个诗,把这个GitHub项目带火了
- 应用Strong Name保存.NET应用程序集
- nginx 1.8.1安装使用
- PHP项目异常类该如何设计,浅谈 PHP 中异常类的使用
- Java中的 BigDecimal,80%的人都用错了....
- simplex字体下载cad_CAD字体大全【资料下载】
- Fxfactory插件:光雾滤镜插件PHYX Stylist
- pda通用扫描app_手持终端PDA盘点机盘点软件盘点APP
- 中国信息消费产业前景动态及未来发展趋势预测报告(2022-2027年)
- 变更控制委员会CCB
- ftok函数的作用:
- Verilog设计_乘法器
- 如何批量将 bmp 格式图片转换转换为 jpg
- 如何实现中英互译?简单的方法介绍
- 云网融合助力大型企业网络底座布局
- import、from import及import as详解
- Owl Carousel轮播插件介绍
- Invalid character found in method name. HTTP method names must be tokens 解决办法
- 在 Android 中执行 Linux 终端命令