python中的subprocess_python函数与方法的详细

python中的subprocess_python函数与方法的详细python3的subprocess的各个方法的区别(-)

大家好,又见面了,我是你们的朋友全栈君。

subprocess(python3.7)

subprocess 主要是为了替换一下的模块函数,允许你执行一些命令,并获取返回的状态码和 输入,输出和错误信息。

os.system
os.spawn*

 

subprocess 有好多方法,本文主要在总结下之间的区别是什么,最后官方推荐使用哪个。

subprocess的主要方法:

subprocess.run(),subprocess.Popen(),subprocess.call   #这些模块都是基于Popen的

Python 3.5 之前

subprocess.call   //call 系列 都是等待命令执行完, Wait for command to complete

subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False, cwd=None, timeout=None)

只返回执行结果的code 等同于subprocess.run(args).returncode

Note Do not use stdout=PIPE or stderr=PIPE with this function. The child process will block if it generates enough output to a pipe to fill up the OS pipe buffer as the pipes are not being read from.

############例子###
ret=subprocess.call(['ls', '-l'])     # ret 程序执行结果返回值,正确执行都是0


----------------------------捕获输出结果到一个文件里Redirecting STDOUT to a File---

with open('joe.txt', 'w') as f:   # 执行后joe.txt 文件内容为 Wed May 15 17:07:59 CST 2019
  subprocess.call(['date'], stdout=f)
   


-------捕获输出结果到字符串,Redirecting STDOUT to strings---

ret=subprocess.check_output()       #ret 为output内容(命令输出的内容) 等同于run(..., check=True, stdout=PIPE).stdout


-------------------
subprocess.check_call()   #和 subprocess.call 一样,只是返回值不是0就引起异常,raise CalledProcessError ,等同于 subprocess.run(…, check=True)



-----输入stdin ----------

with open('joe.txt', 'r') as fr
  subprocess.call(['cat'], stdin=f)
   
-------


######################################################
subprocess.Popen()   #最基本的subprocess模块

class subprocess.Popen(args, bufsize=-1, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=True, shell=False, cwd=None, env=None, universal_newlines=None, startupinfo=None, creationflags=0, restore_signals=True, start_new_session=False, pass_fds=(), *, encoding=None, errors=None, text=None)

p = subprocess.Popen(['ls', '-l']) #返回的p是Popen 对象
p(Popen对象) 有一下方法
p.poll() 检查子进程(cmd) 是否结束 ,如果没有结束会返回None,结束就返回return returncode
p.returncode 程序之后后的返回码,一般正常结束会返回0,否则会返回其他值
p.wait() 等待子进程结束,然后返回returncode值

If the process does not terminate after timeout seconds, raise a TimeoutExpired exception. It is safe to catch this exception and retry the wait.

Note This will deadlock when using stdout=PIPE or stderr=PIPE and the child process generates enough output to a pipe such that it blocks waiting for the OS pipe buffer to accept more data. Use Popen.communicate() when using pipes to avoid that.
Note The function is implemented using a busy loop (non-blocking call and short sleeps). Use the asyncio module for an asynchronous wait: see asyncio.create_subprocess_exec.


p.communicate() 和子进程交互,返回一个元祖,returns a tuple (stdout_data, stderr_data)
​p.terminate() 终止子进程,相当于发送SIGTERM 信号,相当于kill (后面不加参数)
p.kill() 终止子进程,相当于发送SIGKILL 信号,相当于kill -9
p.stdin,p.stdout,p.stderr 分别是输入,输出,错误输出,在python3中都是byte类型需要decode转化
p.returncode() 程序的返回状态 ,程序没有结束的话会返回None
p.pid 返回程序的 pid
p.send_signal(signal) 发送一个信号给子进程
subprocess.Popen(args, bufsize=-1, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=True, shell=False, cwd=None, env=None, universal_newlines=None, startupinfo=None, creationflags=0, restore_signals=True, start_new_session=False, pass_fds=(), *, encoding=None, errors=None, text=None)

Popen 参数:
------
args, a sequence of program arguments or else a single string(参数是一个序列如,列表,元祖等。或者参数是一个字符串),默认情况下,如果args是序列,则要执行的程序是args中的第一项。如果args是一个字符串,需要设置shell=True,即开启一个shell 执行一个字符串的命令,或者序列的第一项
-----
bufsize 参数,stdin,stdout,stderr的缓存参数
executable 参数
@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ,shell=True时 executable=, 用于替换系统默认的shell(一般是bash),
p = subprocess.Popen('echo $0',shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,executable='/bin/zsh') out,_=p.communicate() print(out.decode())
输出>>>/bin/zsh
@@@@@@@@@@@@@@@@@@@@@@@@@ shell=False时 executable=xxx,xxx会替换序列的第一项,一下'ls' 替换了'cat' 最终执行了 ls 'a.py','a.txt','b.txt'
p = subprocess.Popen(['cat','a.py','a.txt','b.txt'],stdout=subprocess.PIPE,stderr=subprocess.PIPE,executable='ls') out,_=p.communicate() print(out.decode())
输出>>>a.py a.txt b.txt

 —–

stdin=None, stdout=None, stderr=None 和输入,输出,错误输出有关,

想要捕捉这些信息可以设置为 将值设置为subprocess.PIPE,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE 如果想要达到2>&1 可以设置为stdout=subprocess.PIPE,stderr=subprocess.STDOUT 这些内容 stdin 如输入(“haha”) 可以通过p.stdin.write(b”hahha”);p.stdin.close() 或者 out,_=p.communicate(b’haha’) stdout,stderr 可以通过p.stdout.readline(),p.stderr.readline() [就是文件描述符的一些参数如read(),readline等]

 

——

 

close_fds=True 子进程是否继承文件描述符,如果false 将关闭除了0,1,2之外的所有文件描述符。 print( p.stdin.fileno()) #查看自身对应的(stdin/stdout)的文件描述符 参考一下遇到的问题close_fds(python2.x遇到的问题???!!!) https://stackoverflow.com/questions/19950185/python-close-fds-not-clear http://drmingdrmer.github.io/tech/programming/2017/11/20/python-concurrent-popen.html

——-

shell=True 是否在shell中执行,参考args 说明

——–

cwd 当前的工作目录,如果有值,就会进入cd 到这个目录执行args命令

—-

text=True,和universal_newlines=True一样,universal_newlines只是为了向后兼容 都是让stdxxx以文本模式输出,而不是默认的二进制模式

If encoding or errors are specified, or text is true, the file objects stdin, stdout and stderr are opened in text mode with the specified encoding and errors, as described above in Frequently Used Arguments. The universal_newlines argument is equivalent to text and is provided for backwards compatibility. By default, file objects are opened in binary mode.

—–

 

python3.5 以后官方推荐使用run

p=subprocess.run(args, ***, stdin=None, input=None, stdout=None, stderr=None, capture_output=False, shell=False, cwd=None, timeout=None, check=False, encoding=None, errors=None, text=None, env=None, universal_newlines=None)

 run 也是需要等进程结束后才返回结果,所以没有结束p.stdout等就不会输出结果

 

run的参数,和Popen 一样

capture_output =true 相当于stdout=PIPE and stderr=PIPE

timeout 设置子进程超时时间,超时引起 TimeoutExpired异常

The timeout argument is passed to Popen.communicate(). If the timeout expires, the child process will be killed and waited for. The TimeoutExpired exception will be re-raised after the child process has terminated

 

input=xxx 相当于 Popen.communicate(xxx)和自动创建 stdin=PIPE

The input argument is passed to Popen.communicate() and thus to the subprocess’s stdin. If used it must be a byte sequence, or a string if encoding or errors is specified or text is true. When used, the internal Popen object is automatically created with stdin=PIPE, and the stdin argument may not be used as well.

check check=True 如果程序返回状态码不是0 就引起异常CalledProcessError

If check is true, and the process exits with a non-zero exit code, a CalledProcessErrorexception will be raised. Attributes of that exception hold the arguments, the exit code, and stdout and stderr if they were captured.

p=subprocess.run(), 执行run后会返回 CompletedProcess类型 实例 P

实例P 有如下方法

p.args

p.returncode

p.stdout

p.stderr

p.check_returncode

 

python中的subprocess_python函数与方法的详细

 

 

 

 

转载于:https://www.cnblogs.com/lijinlei521/p/10873601.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/106820.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • 基于UDP编程_udp详解

    基于UDP编程_udp详解基于UDP编程1UDP是数据报协议,无连接的,不可靠,追求传输效率的一种通信协议数据的发送和接收是同步的.在进行通信之前,不需要建立连接.其传输效率比TCP高.对其服务器而言,并没有三次握手的过程.因此和TCP相比,少了被动监听(listen)和(accept).只需要创建通信设备,绑定IP地址和端口号.然后进行数据的收发.1.服务器端的编程模型创建一个socket端点,返回该端点的文件描述符fdsocket(2)2)将fd和本地地址绑定bind(2)while(1){3)阻塞等待

  • 2020美赛A题解题方法

    2020美赛A题解题方法题目:问题A:向北移动全球海洋温度影响某些海洋生物的栖息地质量。当温度变化太大,它们无法继续繁荣时,这些物种就会迁移到其他更适合它们现在和未来生活和繁殖成功的栖息地。其中一个例子就是美国缅因州的龙虾种群,它们正缓慢地向北迁移到加拿大,那里的海洋温度较低,为它们提供了更合适的栖息地。这种地理种群的转移可能会严重影响依赖海洋生物稳定性的公司的生计。您的团队已被苏格兰北大西洋渔业管理协会聘请为顾问…

  • group by详解

    group by详解一. 概述group_by的意思是根据by对数据按照哪个字段进行分组,或者是哪几个字段进行分组。二. 语法select 字段  from 表名 where  条件  group by    字段或者select 字段  from 表名 group by  字段  having  过滤条件注意:对于过滤条…

  • MYSQL分布式集群使用-主从复制

    MYSQL分布式集群使用-主从复制

  • 修改cmd 命令行中的用户名|C:\Users\下的用户名[通俗易懂]

    修改cmd 命令行中的用户名|C:\Users\下的用户名[通俗易懂]修改→cmd命令行中的用户名|C:\Users\下的用户名1.打开运行输入regedit回车2.定位到HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\WindowsNT\CurrentVersion\ProfileList3.选中下面名字最长的项,双击右侧ProfileImagePath,修改c:\user后的用户名第一步,修改用户名cmdcontro…

  • 借助栈来实现单链表的逆置运算_中缀后缀表达式转换

    借助栈来实现单链表的逆置运算_中缀后缀表达式转换原题链接算术表达式有前缀表示法、中缀表示法和后缀表示法等形式。日常使用的算术表达式是采用中缀表示法,即二元运算符位于两个运算数中间。请设计程序将中缀表达式转换为后缀表达式。输入格式:输入在一行中给出不含空格的中缀表达式,可包含+、-、*、\以及左右括号(),表达式不超过20个字符。输出格式:在一行中输出转换后的后缀表达式,要求不同对象(运算数、运算符号)之间以空格分隔,但结尾不得有多余空格。输入样例:2+3*(7-4)+8/4输出样例:2 3 7 4 – * + 8 4 / +注意

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号