我想從我的 python 腳本中打開一個 powershell,然后在這個新創建的 powershell 中啟動另一個 python 腳本。
import subprocess
subprocess.call("start C:\\Windows\\System32\\WindowsPowerShell\\v1.0", shell=True)
# Here, I would like to write this in the new opened powershell : python ./hello_world.py (then press ENTER)
input("end")
知道我該怎么做嗎?謝謝,祝你有美好的一天!
我嘗試了 subprocess.Popen 通信,但我的新 powershell 中沒有寫入任何內容
uj5u.com熱心網友回復:
你可以使用os.system()
import os
os.system("start powershell python path_to_file")
# some code
uj5u.com熱心網友回復:
另一種選擇:
import subprocess
subprocess.run(["powershell", "-Command", "python hello-world.py"], capture_output=True, cwd="C:\\your\\path")
將輸出:
CompletedProcess(args=['powershell', '-Command', 'python hello-world.py'], returncode=0, stdout=b'hello world\r\n', stderr=b'')
stdout=b'hello world\r\n'你的輸出在哪里。
uj5u.com熱心網友回復:
我認為 subprocess 不打算與活動視窗互動。但要將流資料發送到管道。
嘗試使用pyautogui與 Powershell 視窗進行互動。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/521949.html
