![](/img/trans.png)
[英]How to close command line window after closing script running on web browser?
[英]How to close command window after batch script completion
我創建了一個批處理腳本來運行需要在虛擬環境中運行的 python 腳本。 我在批處理腳本的末尾添加了“退出”,但命令窗口保持打開狀態。 我的腳本如下:
@echo off
cmd /k "C:\Users\user\Documents\venv\Scripts\activate & C:\Users\user\Document\forward_test.py"
exit
我認為這與我如何激活環境有關,但我對批處理腳本的經驗不是很豐富。 有誰知道這里發生了什么?
從cmd /?
:
/C Carries out the command specified by string and then terminates
/K Carries out the command specified by string but remains
由於命令中的/k
,新窗口正在打開,運行您的環境和 python 腳本,然后保持打開狀態。 如果將/k
更改為/c
,則在 python 腳本終止后窗口將關閉。
您已經在 cmd.exe 中運行,因為您調用了一個批處理文件。 因此,您只需要激活您的環境並運行您的 python 腳本:
@Call "%UserProfile%\Documents\venv\Scripts\activate.bat"
@"P:\ath\To\python.exe" "%UserProfile%\Document\forward_test.py"
請注意,您需要為您的python.exe
插入正確的位置(或py.exe
如果您有並喜歡它) ,我使用了您提供的acivate
和forward_test
路徑,請確保您使用正確的路徑, (看起來您提交的代碼可能有一個或多個錯別字) 。
此外,如果 python 腳本在后台運行某些內容(不輸出到控制台並需要一段時間) ,您可能希望將第二行更改為:
@Start "" "P:\ath\To\python.exe" "%UserProfile%\Document\forward_test.py"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.