Python/Basic/chick_python_exam/Unit1_basic/program/execOScmd_subprocess.Popen.py
2024-06-27 15:41:10 +08:00

18 lines
943 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#Program: execOScmd_subprocess.Popen.py (Report bugs/comments to chikh@yuntech.edu.tw)
#Function: 借力使力善用已有的程式助益我們自身工作從鍵盤接受輸入的外部命令名稱透過此Python程式直接執行它
#Note: 可輸入calc (小算盤)、notepad (記事本)、python execOScmd.py、product 10 -2 3 6 等試試看
#import os
import subprocess
print("從鍵盤輸入外部程式名稱、再按<Enter>;在等待輸入時,直接按<Enter>即結束本程式\n")
while True:
cmd = input("輸入指令 ==> ")
if cmd == '' or cmd == "exit": break;
#os.system(cmd) #執行外部程式
#os.popen(cmd)
#result = subprocess.Popen(["ls", "-l"], stdout=subprocess.PIPE)
#output = result.communicate()[0] # 獲取命令的輸出
#print(output.decode("utf-8") # 將輸出列印出來
subprocess.Popen(['start', 'cmd', '/c', cmd], shell=True) #啟動外部程式,另以一獨立視窗呈現運作效果