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
- python3下urllib案例(URL Rewrite Module 2.1 URL重写模块规则写法)
- python机器人开发(python程序控制NAO机器人行走)
- python人脸识别实战视频(Python学习笔记之图片人脸检测识别实例教程)
- python和mysql实战(由Python编写的MySQL管理工具代码实例)
- python脚本压缩包解密(详解Python 解压缩文件)
- python 获取天气预报(详解用python自制微信机器人,定时发送天气预报)
- python3正则表达式详解(Python正则表达式和re库知识点总结)
- python3 怎么查看函数用法(Python3 max函数基础用法)
- python如何对参数长度进行限制(python 多个参数不为空校验方法)
- python表白神器教程(python浪漫表白源码)
- python实现数据的预测(Python基于scipy实现信号滤波功能)
- pythonmatplotlib散点图怎么画(python使用matplotlib画柱状图、散点图)
- pythontime模块有哪些(Python3.5内置模块之time与datetime模块用法实例分析)
- python opencv 标记目标(使用Python的OpenCV模块识别滑动验证码的缺口推荐)
- pythonturtle库画图代码(用Python中的turtle模块画图两只小羊方法)
- python批量自动化访问网站(python 自动批量打开网页的示例)
- 越南特产(越南特产首饰)
- TVB负评王连续挑战警察角色《使徒行者3》中将饰演卧底(TVB负评王连续挑战警察角色使徒行者3中将饰演卧底)
- 《精英律师》剧照首曝光,实力演员飙戏,演绎律政职场百态(精英律师剧照首曝光)
- 靳东领衔打造高精职场 新丽出品《精英律师》曝定妆照(靳东领衔打造高精职场)
- 靳东新剧《精英律师》定档,众星云集,这剧可追(靳东新剧精英律师定档)
- 精英律师 廖佳敏封印恋情曝光,顾婕马失前蹄 你个老不死的(廖佳敏封印恋情曝光)
热门推荐
- php-fpm配置文件在哪里(PHP-FPM 设置多pool及配置文件重写操作示例)
- webpack打包公共文件(webpack文件打包错误异常)
- dockermysql配置详解(Docker 部署Mysql 服务和Redis 服务的方法)
- 织梦cms模块使用方法(织梦CMS文章模型文章转入新的自定义模型的方法)
- vmware使用教程win10(VMware Workstation安装并安装WIN10操作系统连接外网步骤指导超详细教程)
- laravel获取访问来路的函数(Laravel实现搜索的时候分页并携带参数)
- mysql中的null是什么(浅谈为什么Mysql数据库尽量避免NULL)
- asp.net 日期格式化
- iis安全包括哪些方面(使用华盾IIS备份还原工具备份还原IIS站点图解)
- js如何将json字符串转换为json对象
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9