當前python程式執行過程中執行其它的python程式(使用內建庫subprocess)
阿新 • • 發佈:2022-12-02
import subprocess import time # # subprocess.run(args, *, stdin=None, input=None, stdout=None, stderr=None, capture_output=False, shell=False, # cwd=None, timeout=None, check=False, encoding=None, errors=None, text=None, env=None, # universal_newlines=None) # 當子程序執行完才會執行後續主程序的程式碼# subprocess.run("python scratch.py", shell=True) # print("scratch.py執行完才會執行我") # 預設不會等子程序執行完就會執行後續主程序的程式碼 如果主程序的程式碼執行完子程序沒有執行完也會被終止 # s = subprocess.Popen("python scratch.py", shell=True) # print("我不會等scratch.py執行完才執行,我先執行") # s物件引用了wait方法 會等待子程序執行完才會執行後續主程序的程式碼 # s = subprocess.Popen("python scratch.py", shell=True)# s.wait() # print("scratch.py執行完才會執行我") # cwd屬性值用來切換到執行子程序程式碼的當前目錄下 # s = subprocess.Popen("python scratch.py", shell=True, cwd="./h") # s.wait() # print("scratch.py執行完才會執行我")
參考文章:https://www.runoob.com/w3cnote/python3-subprocess.html