簡體   English   中英

如何執行沒有“.exe”擴展名的文件

[英]How to execute a file without the ".exe" extension

我有一個文件:“abc”,它是一個可執行文件。 我想通過 Phthon 或 windows CMD 執行它。

如果我寫代碼:

subprocess.Popen('-a -b -c', creationflags=0x08, shell=True, executable="C:\\abc")

然后,abc 執行,但 params(-a -b -c) 被忽略

所以……我怎樣才能重新愛上它?

為什么不將您的代碼更改為以下版本

args = ['C:\\abc', '-a', '-b', '-c']

p = subprocess.Popen(' '.join(args), shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

或者沒有shell=True你可以進一步減少它

p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

或者您可以使用另一種方法從您的 exe 獲取輸出並將參數傳遞給它

Output = subprocess.check_output(' '.join(args), shell=True).decode()

在 PowerShell 中

& "C:\abc.exe"

最后,我自己找到了答案,就是使用win32process.CreateProcess感謝其他作者,但他們的答案是錯誤的。

正確答案是:

win32process.CreateProcess(r"C:\abc", "-a -b -c", None, None, 0, 0, None, None, win32process.STARTUPINFO())

以下答案不起作用:

subprocess.Popen('-a -b -c', creationflags=0x08, shell=True, executable="C:\\abc")

#or

p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

也許我真的不知道如何使用subprocess ,但Windows真的很特別。 沒有后綴的可執行文件不能直接在命令行上執行。

暫無
暫無

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

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