Python 常用函数 subprocess模块
subprocess模块用来生成子进程,并且可以通过管道连接它们的输入、输出、错误,以及获得它们的返回值
subprocess包主要功能是执行外部的命令和程序,与shell类似
import subprocess #执行系统命令 subprocess.Popen(r'dir C:\Users',shell=True) #管道,标准输出 res = subprocess.Popen(r'dir C:\Users',shell=True,stdout=subprocess.PIPE) print(res.stdout.read().decode('gbk')) #管道,错误输出 res = subprocess.Popen(r'ls C:\Users',shell=True,stderr=subprocess.PIPE) print(res.stderr.read().decode('gbk')) #管道,标准输入 res1 = subprocess.Popen(r'dir F:\Python\Code\test',shell=True,stdout=subprocess.PIPE) res2 = subprocess.Popen(r'findstr xml',shell=True,stdin=res1.stdout,stdout=subprocess.PIPE) print(res2.stdout.read().decode('gbk'))