能夠經過python代碼給操做系統終端發送命令,並能夠返回結果python
sub:子shell
process:進程操作系統
import subprocess while True: # 一、讓用戶輸入終端命令 cmd_str = input('請輸入終端命令:') # 二、調用subprocess中.Popen(命令, shell=True, stdout=subprocess.PIPE, sdterr=subprocess.PIPE)獲得一個對象 obj = subprocess.Popen(cmd_str, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # 三、獲取成功時的返回值 success = obj.stdout.read().decode('gbk') if success: print(success) # 四、獲取失敗時的返回值 error = obj.stderr.read().decode('gbk') if error: print(error)