python数据分析用到的模块(python模块之subprocess模块级方法的使用)
python数据分析用到的模块
python模块之subprocess模块级方法的使用subprocess.run()
运行并等待args参数指定的指令完成,返回completedprocess实例。
参数:(*popenargs, input=none, capture_output=false, timeout=none, check=false, **kwargs)。除input, capture_output, timeout, check,其他参数与popen构造器参数一致。
capture_output:如果设置为true,表示重定向stdout和stderr到管道,且不能再传递stderr或stdout参数,否则抛出异常。
input:input参数将作为子进程的标准输入传递给popen.communicate()方法,必须是string(需要指定encoding或errors参数,或者设置text为true)或byte类型。非none的input参数不能和stdin参数一起使用,否则将抛出异常,构造popen实例的stdin参数将指定为subprocess.pipe。
timeout:传递给popen.communicate()方法。
check:如果设置为true,进程执行返回非0状态码将抛出calledprocesserror异常。
|
# 源码 def run( * popenargs, input = none, capture_output = false, timeout = none, check = false, * * kwargs): if input is not none: if 'stdin' in kwargs: raise valueerror( 'stdin and input arguments may not both be used.' ) kwargs[ 'stdin' ] = pipe if capture_output: if ( 'stdout' in kwargs) or ( 'stderr' in kwargs): raise valueerror( 'stdout and stderr arguments may not be used ' 'with capture_output.' ) kwargs[ 'stdout' ] = pipe kwargs[ 'stderr' ] = pipe with popen( * popenargs, * * kwargs) as process: try : stdout, stderr = process.communicate( input , timeout = timeout) except timeoutexpired: process.kill() stdout, stderr = process.communicate() raise timeoutexpired(process.args, timeout, output = stdout, stderr = stderr) except : # including keyboardinterrupt, communicate handled that. process.kill() # we don't call process.wait() as .__exit__ does that for us. raise retcode = process.poll() if check and retcode: raise calledprocesserror(retcode, process.args, output = stdout, stderr = stderr) return completedprocess(process.args, retcode, stdout, stderr) |
python3.5版本前,call(), check_all(), checkoutput()三种方法构成了subprocess模块的高级api。
subprocess.call()
运行并等待args参数指定的指令完成,返回执行状态码(popen实例的returncode属性)。
参数:(*popenargs, timeout=none, **kwargs)。与popen构造器参数基本相同,除timeout外的所有参数都将传递给popen接口。
调用call()函数不要使用stdout=pipe或stderr=pipe,因为如果子进程生成了足量的输出到管道填满os管道缓冲区,子进程将因不能从管道读取数据而导致阻塞。
|
# 源码 def call( * popenargs, timeout = none, * * kwargs): with popen( * popenargs, * * kwargs) as p: try : return p.wait(timeout = timeout) except : p.kill() p.wait() raise |
subprocess.check_call()
运行并等待args参数指定的指令完成,返回0状态码或抛出calledprocesserror异常,该异常的cmd和returncode属性可以查看执行异常的指令和状态码。
参数:(*popenargs, **kwargs)。全部参数传递给call()函数。
注意事项同call()
|
# 源码 def check_call( * popenargs, * * kwargs): retcode = call( * popenargs, * * kwargs) if retcode: cmd = kwargs.get( "args" ) if cmd is none: cmd = popenargs[ 0 ] raise calledprocesserror(retcode, cmd) return 0 |
subprocess.check_output()
运行并等待args参数指定的指令完成,返回标准输出(completedprocess实例的stdout属性),类型默认是byte字节,字节编码可能取决于执行的指令,设置universal_newlines=true可以返回string类型的值。
如果执行状态码非0,将抛出calledprocesserror异常。
参数:(*popenargs, timeout=none, **kwargs)。全部参数传递给run()函数,但不支持显示地传递input=none继承父进程的标准输入文件句柄。
要在返回值中捕获标准错误,设置stderr=subprocess.stdout;也可以将标准错误重定向到管道stderr=subprocess.pipe,通过calledprocesserror异常的stderr属性访问。
|
# 源码 def check_output( * popenargs, timeout = none, * * kwargs): if 'stdout' in kwargs: raise valueerror( 'stdout argument not allowed, it will be overridden.' ) if 'input' in kwargs and kwargs[ 'input' ] is none: # explicitly passing input=none was previously equivalent to passing an # empty string. that is maintained here for backwards compatibility. kwargs[ 'input' ] = ' ' if kwargs.get(' universal_newlines ', false) else b' ' return run( * popenargs, stdout = pipe, timeout = timeout, check = true, * * kwargs).stdout |
subprocess模块还提供了python2.x版本中commands模块的相关函数。
subprocess.getstatusoutput(cmd)
实际上是调用check_output()
函数,在shell中执行string类型的cmd指令,返回(exitcode, output)
形式的元组,output(包含stderr
和stdout
)是使用locale encoding解码的字符串,并删除了结尾的换行符。
|
# 源码 try : data = check_output(cmd, shell = true, universal_newlines = true, stderr = stdout) exitcode = 0 except calledprocesserror as ex: data = ex.output exitcode = ex.returncode if data[ - 1 :] = = '\n' : data = data[: - 1 ] return exitcode, data |
subprocess.getoutput(cmd)
与getstatusoutput()
类似,但结果只返回output。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持开心学习网。
原文链接:https://segmentfault.com/a/1190000018658746
- python数据存取(python异步存储数据详解)
- python字符处理的函数(Python字符串内置函数功能与用法总结)
- 用python查看运行进程(在Python运行时动态查看进程内部信息的方法)
- python字符串分析总结(Python 存储字符串时节省空间的方法)
- python 配置文件环境变量(python环境路径配置以及命令行运行脚本)
- python对象创建流程(Python3.5面向对象与继承图文实例详解)
- python获取企业微信信息(通过shell+python实现企业微信预警)
- python中如何遍历键(Python中按值来获取指定的键)
- python列表精讲33节(Python列表知识应知应会)
- python发送微信消息脚本(python实现给微信指定好友定时发送消息)
- python数据分析pandas教程(11个Python Pandas小技巧让你的工作更高效附代码实例)
- python怎么用代码写出心形(六行python代码的爱心曲线详解)
- opencv提取图像轮廓python代码(Python基于opencv调用摄像头获取个人图片的实现方法)
- python迭代列表解析(Python可迭代对象操作示例)
- python中的变量与变量类型(Python变量类型知识点总结)
- python获取excelsheet名称(python查询文件夹下excel的sheet名代码实例)
- 浙江省一个县,人口超40万,建县历史超1100年(浙江省一个县人口超40万)
- 五代十国南唐历代国君(五代十国南唐历代国君)
- 飞机引进工程师杨隆 匠人匠心,只争朝夕(飞机引进工程师杨隆)
- 三人行,她们是育人路上的 铁三角 团队(她们是育人路上的)
- 阴阳师 孟婆山兔CP不倒 新皮肤草稿 孟婆兔 让痒痒鼠点赞(阴阳师孟婆山兔CP不倒)
- 阴阳师孟婆御魂推荐 孟婆御魂搭配毕业套(阴阳师孟婆御魂推荐)
热门推荐
- sql语句按字段排序(SQL语句实现表中字段的组合累加排序)
- sqlserver使用简介(SQL Server Page结构深入分析)
- laravel数据返回格式(laravel 关联关系遍历数组的例子)
- python在mysql创建数据库(python3对接mysql数据库实例详解)
- element中表单验证规则(element多个表单校验的实现)
- php自定义函数返回多少个值(PHP7导出Excel报ERR_EMPTY_RESPONSE解决方法)
- vuekeep-alive源码(vue中keepAlive组件的作用和使用方法详解)
- webpack常用的配置(webpack-dev-server搭建本地服务器的实现)
- 如何让tomcat启动更快(快速解决Tomcat启动慢的问题,超简单)
- JS函数前面感叹号的作用
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9