模块讲解----subprocess模块
历史
#输出结果到屏幕上,并不返回执行状态
os.system('dir')
#保存命令的执行结果输出
ret = os.popen('dir').read()
问题:上面2条是把命令结果保存下来了,但是返回状态没了,也就没办法判断该命令是否执行成功。例如:

解决方式: 既想要执行结果邮箱要执行状态请使用subprocess模块。
注意:
在python2.7里有个commands模块,可以通过commands.getstatusoutput("dir")查看系统命令的执行状态,但是在3.5里就已经弃用了。
在3.5里新出了一个subprocess模块来替换os.system、os.spawn*、 commands等模块的功能。
subprocess模块:
作用:
#python解析shell命令:
subprocess.run(["df","-h"]) #不用python解析shell命令:(shell=True)
subprocess.run("df -h |grep sda1",shell=True)
一、执行命令,返回状态值,不返回结果:(call)
1、列表形式(shell = False)
ret = subprocess.call(["ls","-l"],shell=False)
print(ret) 2、字符串形式(shell=True)
ret = subprocess.call("ls -l",shell=True)
print(ret)
二、执行命令,如果状态码是0,则返回0,否则报异常,不反悔结果:(check_call)
1、列表形式(shell = False)
ret = subprocess.check_call(["ls","-l"],shell=False)
print(ret) 2、字符串形式(shell=True)
ret = subprocess.check_call("ls -l",shell=True)
print(ret)
三、执行命令,如果状态码是 0 ,则返回执行结果,否则抛异常:(check_output)
1、列表形式(shell = False)
ret = subprocess.check_output(["echo","hello world"],shell=False)
print(ret) 2、字符串形式(shell=True)
ret = subprocess.check_output("exit 1",shell=True)
print(ret)
四、本地创建目录:
使用subprocess.Popen(...) 用于执行复杂的系统命令(上面的方法在后台实际就是调用的Popen)
(二)、输入即可得到输出,如:ifconfig
import subprocess
ret1 = subprocess.Popen(["mkdir","t1"])
ret2 = subprocess.Popen("mkdir t2", shell=True)
五、跳转到指定目录然后在创建目录:
输入进行某环境,依赖再输入,如:python
obj = subprocess.Popen("mkdir t3", shell=True, cwd='/home/dev',)
六、通过管道进入交互方式:
import subprocess #下面的stdin,stdout,stderr相当于三个管道,以后要想写内容的时候通过管道就可以写。
obj = subprocess.Popen(["python"], #写了一个python,就会进入python解释器,obj就相当于定义一个对象。
stdin=subprocess.PIPE, #专门写的管道1
stdout=subprocess.PIPE, #那正常结果的管道2
stderr=subprocess.PIPE, #用来拿错误的管道3
universal_newlines=True) #向管道里面写数据,下面2个print就相当于写了2行数据。(相当于在终端把命令输入进去了,剩下的就是拿结果)
obj.stdin.write("print(1)\n")
obj.stdin.write("print(2)")
obj.stdin.close() #表示停止写入 #可以到stdout的管道中取结果,读到正常输出的结果值
cmd_out = obj.stdout.read()
obj.stdout.close() #如果出现错误,可以到stderr中提取报错结果
cmd_error = obj.stderr.read()
obj.stderr.close() #最后输出执行命令结果
print(cmd_out)
print(cmd_error)
七、合并stdout和stderr的输入结果值:(communicate):
import subprocess obj = subprocess.Popen(["python"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True)
obj.stdin.write("print(1)\n")
obj.stdin.write("print(2)") out_error_list = obj.communicate() #其实这个命令就是去stdout和stderr两个管道里去拿返回out和error值,不用再次编辑。
print(out_error_list)
八、执行简单的命令:
import subprocess
obj = subprocess.Popen(["python"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True)
out_error_list = obj.communicate('print("hello")')
print(out_error_list)
模块讲解----subprocess模块的更多相关文章
- python重要模块之subprocess模块
python重要模块之subprocess模块 我们经常要通过python去执行系统的命令或者脚本,系统的shell命令是独立于你的python进程之外的,每执行一条命令,就相当于发起了一个新的进程, ...
- configparser模块,subprocess 模块,xlrd,xlwt ,xml 模块,面向对象
1. configparser模块 2.subprocess 模块 3.xlrd,xlwt 4.xml 模块 5.面向对象 面向对象是什么? 是一种编程思想,指导你如何更好的编写代码 关注点在对象 具 ...
- [xml模块、hashlib模块、subprocess模块、os与sys模块、configparser模块]
[xml模块.hashlib模块.subprocess模块.os与sys模块.configparser模块] xml模块 XML:全称 可扩展标记语言,为了能够在不同的平台间继续数据的交换,使交换的数 ...
- logging模块、shutil模块、subprocess模块、xml模块
logging模块 shutil模块 subprocess模块 xml模块 logging模块 函数式简单配置 import logging logging.debug('debug message' ...
- python-re模块和subprocess模块
一.re模块 re中文为正则表达式,是字符串处理的常用工具,通常用来检索和替换符合某个模式的文本. 注:要搜索的模式和字符串都可以是unicode字符串(str)和8位字符串(bytes),但是不能将 ...
- os模块、os.path模块、shutil模块、configparser模块、subprocess模块
一.os模块 os指的是操作系统 该模块主要用于处理与操作系统相关的操作,常用的是文件操作(读.写.删.复制.重命名). os.getcwd() 获取当前文件所在的文件夹路径 os.chdir() ...
- re模块与subprocess模块介绍
一:re模块 处理正则表达式的模块,正则表达式就是一些带有特殊含义的符号或者符号的组合. 作用:对字符串进行过滤,在一堆字符串中找到你所关心的内容,你就需要告诉计算机你的过滤的 规则是什么 ...
- Python开发基础-Day15正则表达式爬虫应用,configparser模块和subprocess模块
正则表达式爬虫应用(校花网) import requests import re import json #定义函数返回网页的字符串信息 def getPage_str(url): page_stri ...
- python16_day06【类、RE模块、subprocess模块、xml模块、shelve模块】
一.shelve模块 import shelve # 基于pickle模块, d = shelve.open('shelve_test') class Test(object): def __init ...
随机推荐
- app服务端server端数据库设计
- mybatise 动态sql
1. <if><choose> 动态sql 相当 <if> Java if 满足多个条件 <choose> <when> java ...
- spring+ehcache实战--性能优化之道
在做系统集成平台项目的时候遇到了一个比較麻烦的问题.原因是使用考试系统的时候所依赖的是基础系统公布的webservice来获取基础数据,webservice的跨网络传输本身或多或少会对系统性能产生一定 ...
- cocos2d-x游戏引擎核心之三——主循环和定时器
一.游戏主循环 在介绍游戏基本概念的时候,我们曾介绍了场景.层.精灵等游戏元素,但我们却故意避开了另一个同样重要的概念,那就是游戏主循环,这是因为 Cocos2d 已经为我们隐藏了游戏主循环的实现.读 ...
- <转>Logistic回归总结
转自http://blog.csdn.net/dongtingzhizi/article/details/15962797 当我第一遍看完台大的机器学习的视频的时候,我以为我理解了逻辑回归,可后来越看 ...
- poj_2406 kmp
题目大意 给出一个字符串S,S可能是多个较短的字符串的重复连接.比如"ababab"为字符串"ab"重复三次的结果:"abc" 为" ...
- const T* 和 T* const
使用c++的时候,经常会在 const int *p 和 int * const p这个地方迷惑.这里记录一下: const int *p = int const *p //这里const后面的为* ...
- pythonday
python学习 http://www.cnblogs.com/fengyuhao/p/7168903.html pythonday http://www.cnblogs.co ...
- [UML]UML 教程 - 第二部分
UML作为软件开发典型的开发过程 业务过程模型创建 业务过程模型被用来定义发生在企业或组织内部的高级业务活动和业务过程,并且是建立用例模型的基础.一般来说业务过程模型比一个软件系统所能实现的更多(比如 ...
- 微信小程序 --- 事件绑定
事件类别: tap:点击事件: longtap:长按事件: touchstart:触摸开始: touchend:触摸结束: touchcansce:取消触摸: 事件绑定: bind绑定: catch绑 ...