subprocess

 

 

>>> import subprocess
>>> proc = subprocess.Popen('ls | wc -l', stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=1, shell=True )
>>> proc.wait()
0
>>> print(proc)
<subprocess.Popen object at 0x7f9f985bb710>
>>> print(proc.communicate())
('178\n', None)

  

 

https://www.cnblogs.com/security-darren/p/4733368.html

 

subprocess模块的其他属性

subprocess.PIPE

  调用本模块提供的若干函数时,作为 std* 参数的值,为标准流文件打开一个管道。

例4:

  使用管道连接标准流文件

1
2
3
4
5
6
7
import subprocess
child1 = subprocess.Popen(['ls''-l'], stdout=subprocess.PIPE)
child2 = subprocess.Popen(['wc''-l'], stdin=child1.stdout, stdout=subprocess.PIPE)
out = child2.communicate()
child1.wait()
child2.wait()
print(out)

  这里将子进程 child1 的标准输出作为子进程 child2 的标准输入,父进程通过 communicate() 读取 child2 的标准输出后打印

posted on 2020-12-10 18:35  cdekelon  阅读(75)  评论(0编辑  收藏  举报

导航