subprocess模块
subprocess可以根据命令的结果,正确的或者错误的分开存放
存放正确的结果
# import subprocess # obj=subprocess.Popen('tasklist',shell=True, # stdout=subprocess.PIPE, # stderr=subprocess.PIPE, # )# print(obj.stdout.read().decode('gbk')) #编码处理
存放错误的结果
# import subprocess # import time # obj=subprocess.Popen('taaaaaaaaaaaaaaaasklist',shell=True, # stdout=subprocess.PIPE, # stderr=subprocess.PIPE, # ) # print(obj.stderr.read().decode('gbk')) # # 'taaaaaaaaaaaaaaaasklist' 不是内部或外部命令,也不是可运行的程序或批处理文件。
实现命令的过滤例如 tasklist|findstr python
import subprocess obj1=subprocess.Popen('tasklist',shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,) obj2=subprocess.Popen('findstr python',shell=True,stdin=obj1.stdout,stdout=subprocess.PIPE,stderr=subprocess.PIPE,) print(obj2.stdout.read().decode('gbk')) #python.exe 16988 Console 24 11,216 K
相当于
import subprocess obj1=subprocess.Popen('tasklist|findstr python',shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,)
posted on 2018-07-12 12:24 dawn-liu 阅读(...) 评论(...) 编辑 收藏