subprocess 模块

import subprocess
# 就用来执行系统命令
import os

cmd = r'dir D:上海python全栈4期day23 | findstr "py"'
# res = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
# # 从管道中读取数据   管道就是 两个进程通讯的媒介
# # print(type(res.stdout.read().decode("GBK")))
# print(res.stdout.read().decode("GBK"))
# print(res.stderr.read().decode("GBK"))

dir = r'dir D:上海python全栈4期day23'
find = 'findstr "py"'
"""
stdout 输出管道
stdin 输入管道
stderr 错误管道
"""
res1 = subprocess.Popen(dir,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)

res2 = subprocess.Popen(find,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,stdin=res1.stdout)
# 从管道中读取数据   管道就是 两个进程通讯的媒介
# print(type(res.stdout.read().decode("GBK")))
# print(res1.stdout.read().decode("GBK"))
print(res2.stderr.read().decode("GBK"),"33333")



# 简单总结  subprocess 主要用于执行系统指令 (启动子进程) 与os.system的不同在于
# subprocess 可以与这个子进程进行数据交换
原文地址:https://www.cnblogs.com/TF511/p/9821421.html