簡體   English   中英

在 Ubuntu/WSL 中執行 Python 中的 an.exe

[英]Execute an .exe in Python in Ubuntu/WSL

如何在 WSL 中的 Ubuntu 上使用 Python3 執行.exe文件? 從我的搜索中,我找到os.system ,但盡管放置在正確的文件夾中,但我找不到.exe文件。 我也試過os.open沒有結果。

import os


current = os.chdir('../../../Programmi/OWASP/Zed Attack Proxy/')
os.system("ZAP.exe")

嘗試使用完全限定的路徑:

os.system("../../../Programmi/OWASP/Zed Attack Proxy/ZAP.exe")

為了稍微解釋@MichaelMatsaev的回答,您嘗試對問題中的 Python 示例進行的操作與此 Bash 構造基本相同:

cd ../../../Programmi/OWASP/Zed Attack Proxy/
ZAP.exe

你會得到一個從 Bash command not found 幾乎每個 Linux 應用程序都將以相同的方式工作,因為它們最終都在exec系列中使用某種形式的系統調用。

在 Linux(不僅僅是 WSL 中的 Windows .exe )中執行任何二進制文件時,該二進制文件必須是:

  • 在搜索$PATH
  • 指定二進制文件的完全限定(相對或絕對)路徑。

因此,除了@MichaelMatsaev 的(正確)建議使用:

os.system("../../../Programmi/OWASP/Zed Attack Proxy/ZAP.exe")

以下方法也可以:

os.chdir('../../../Programmi/OWASP/Zed Attack Proxy/')
os.system("./ZAP.exe")

而且,雖然在這種情況下這有點病態(即我無法想象你會想要這樣做),但你甚至可以修改代碼中的搜索路徑,然后在沒有完全限定路徑的情況下調用二進制文件:

os.environ['PATH'] += os.pathsep + '../../../Programmi/OWASP/Zed Attack Proxy/'
os.system("ZAP.exe")

旁注:AFAIK,沒有理由嘗試將os.chdir存儲到current變量中,因為os.chdir不返回值。

暫無
暫無

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

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