簡體   English   中英

從 Windows 批處理文件運行時,如何從 exe 中獲取 Python sys.exit() 代碼?

[英]How do I get the Python sys.exit() code from an exe when running it from a Windows batch file?

我有一個像這樣的 Python 程序:

import sys

exitCode = 2

if(sys.argv.__len__() > 1):
    exitCode = sys.argv[1]

print('exit code is ' + str(exitCode))  

sys.exit(exitCode)

我從一個 .bat 文件中調用它,如下所示:

@echo off
cd C:\Users\bmq22\projects\Python\ExitCodeTesting\dist
main.exe 10
echo return code is: %ERRORLEVEL%
pause

我希望 %ERRORLEVEL% 將包含來自 exe 的返回代碼(在本例中為 10),但它沒有。 這是 .bat 文件中的 output:

exit code is 10
10
return code is: 1
Press any key to continue . . .

所以我可以在屏幕上看到我的 Python 返回代碼,但我不知道如何捕獲它以在批處理文件中使用。 任何幫助表示贊賞。

更新:我是個白痴。 需要在退出之前將退出代碼參數轉換為 int。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM