Python模組subprocess

來源:互聯網
上載者:User

標籤: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.