標籤:close tde bsp RoCE str proc klist opened 命令
subprocess的常用用法
1 """ 2 Description: 3 Author:Nod 4 Date: 5 Record: 6 #---------------------------------v1-----------------------------------# 7 """ 8 9 import subprocess10 import time11 12 # 正確的命令通過管道輸出13 obj = subprocess.Popen(‘ping 127.0.0.1‘, shell=True,14 stdout=subprocess.PIPE,15 stderr=subprocess.PIPE,16 )17 print(‘\033[31;1m執行結果1\033[0m‘)18 print(obj.stdout.read().decode(‘gbk‘))19 20 # 不正確的命令通過管道輸出21 obj = subprocess.Popen(‘12ping 127.0.0.1‘, shell=True,22 stdout=subprocess.PIPE,23 stderr=subprocess.PIPE,24 25 )26 print(‘\033[31;1m執行結果2\033[0m‘)27 print(obj.stderr.read().decode(‘gbk‘))28 29 # 執行一串命令的方式1 tasklist | findstr python30 obj = subprocess.Popen(31 ‘tasklist | findstr python‘, shell=True,32 stdout=subprocess.PIPE, # 命令的正確結果進入管道33 stderr=subprocess.PIPE, # 命令的錯誤結果進入另外1個管道34 35 )36 print(‘\033[31;1m執行結果3\033[0m‘)37 print(obj.stdout.read().decode(‘gbk‘))38 39 # 執行一串命令的方式240 obj2 = subprocess.Popen(41 ‘tasklist‘, shell=True,42 stdout=subprocess.PIPE,43 stderr=subprocess.PIPE,44 )45 # 此處會將obj2的執行結果輸入給obj2 stdin=obj2.stdout,46 obj3 = subprocess.Popen(47 ‘findstr python‘,48 shell=True,49 stdin=obj2.stdout,50 stdout=subprocess.PIPE,51 stderr=subprocess.PIPE,52 )53 print(‘\033[31;1m執行結果4\033[0m‘)54 print(obj3.stdout.read().decode(‘utf-8‘))
View Code
Python模組subprocess