模块讲解----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 ...
随机推荐
- visual studio 2017使用NHibernate4.0连接oracle11g数据库
之前一直是公司用NHibernate2.1来做项目,连接oracle 10g的数据库,配置NHibernate的东西都是以前的同事做好了的,也怪自己太懒了,没尝试过配置这个东西,虽然一直在使用NHib ...
- 关于直播学习笔记-003-nginx-rtmp、srs、vlc、obs
服务器 1.nginx-rtmp:https://github.com/illuspas/nginx-rtmp-win32 2.srs:https://github.com/illuspas/srs- ...
- linux中的目录和文件的统计
ls 目录 | wc -l find ./ -type d | wc -l //查找目录个数 find ./ -type f | wc -l ...
- ActiveMQ内存配置和密码设置
1.配置内存 bin中activemq.bat 中的第一行 加上 : REM 配置内存 set ACTIVEMQ_OPTS=-Xms1G -Xmx1G 2.修改控制台密码 1.打开conf/jetty ...
- react实现的点击拖拽元素效果
之前用vue做日程管理组件的时候,用到了点击拖拽的效果,即点击元素,鼠标移动到哪里,元素移动到哪里,鼠标松开,拖拽停止,现在在弄react,于是也在想实现这个效果,经过一番折腾,效果出来了,代码如下: ...
- poj_1988 并查集
题目大意 开始有N堆砖块,编号为1,2....N,每堆都只有一个.之后可以进行两种操作: (1)M X Y 将编号为X的砖块所在的那堆砖拿起来放到编号为Y的砖块所在的堆上: (2)C X 查询编号为X ...
- struts2基础---->自定义拦截器
这一章,我们开始struts2中拦截器的学习. 自定义拦截器
- js部署中如何提高页面加载速度
1.合并js文件,减少http请求数量. 2.对js文件进行压缩 3.以gzip的形式提供js 4.使js文件可缓存 5.使用CDN
- 【Android M】预制的 Google GMS包
目录:android/vendor/google/apps .├── AndroidPay│ ├── Android.mk│ ├── AndroidPay_arm64.apk│ ├── A ...
- 【Android N 7.1.1】 ActivityManagerService 获取cpu状态
void updateCpuStatsNow() { synchronized (mProcessCpuTracker) { mProcessCpuMutexFree.set(false); fina ...