Common usage of subprocess
1 """2 Description:3 Author:nod4 Date:5 Record:6 #---------------------------------v1-----------------------------------#7 """8 9 ImportsubprocessTen Import Time One A #the correct command is output through the pipeline -obj = subprocess. Popen ('Ping 127.0.0.1', shell=True, -stdout=subprocess. PIPE, theStderr=subprocess. PIPE, - ) - Print('\033[31;1m Execution Results 1\033[0m') - Print(Obj.stdout.read (). Decode ('GBK')) + - #incorrect command output through the pipeline +obj = subprocess. Popen ('12ping 127.0.0.1', shell=True, Astdout=subprocess. PIPE, atStderr=subprocess. PIPE, - - ) - Print('\033[31;1m Execution Results 2\033[0m') - Print(Obj.stderr.read (). Decode ('GBK')) - in #how to execute a sequence of commands 1 tasklist | findstr python -obj =subprocess. Popen ( to 'tasklist | findstr python', shell=True, +Stdout=subprocess. PIPE,#the correct result of the command enters the pipeline -Stderr=subprocess. PIPE,#Command error results into 1 additional pipelines the * ) $ Print('\033[31;1m Execution Results 3\033[0m')Panax Notoginseng Print(Obj.stdout.read (). Decode ('GBK')) - the #how to execute a sequence of commands 2 +Obj2 =subprocess. Popen ( A 'tasklist', shell=True, thestdout=subprocess. PIPE, +Stderr=subprocess. PIPE, - ) $ #The results of obj2 execution are entered here to Obj2 Stdin=obj2.stdout, $Obj3 =subprocess. Popen ( - 'findstr python', -Shell=True, thestdin=Obj2.stdout, -stdout=subprocess. PIPE,WuyiStderr=subprocess. PIPE, the ) - Print('\033[31;1m Execution Results 4\033[0m') Wu Print(Obj3.stdout.read (). Decode ('Utf-8'))
View Code
Python module subprocess