简体   繁体   English

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

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

How can I execute an .exe file with Python3 on Ubuntu in WSL?如何在 WSL 中的 Ubuntu 上使用 Python3 执行.exe文件? From my searches I found os.system , but despite being placed in the correct folder, I cannot find the .exe file.从我的搜索中,我找到os.system ,但尽管放置在正确的文件夹中,但我找不到.exe文件。 I also tried with os.open with no results.我也试过os.open没有结果。

import os


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

Try using a fully-qualified path:尝试使用完全限定的路径:

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

To expound on @MichaelMatsaev's answer a bit, what you are attempting to do with the Python example in your question is essentially the same as this Bash construct:为了稍微解释@MichaelMatsaev的回答,您尝试对问题中的 Python 示例进行的操作与此 Bash 构造基本相同:

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

You'll get a command not found from Bash.你会得到一个从 Bash command not found Pretty much every Linux app will work the same way, since they all ultimately use some form of syscall in the exec family.几乎每个 Linux 应用程序都将以相同的方式工作,因为它们最终都在exec系列中使用某种形式的系统调用。

When executing any binary in Linux (not just Windows .exe 's in WSL), the binary must be either:在 Linux(不仅仅是 WSL 中的 Windows .exe )中执行任何二进制文件时,该二进制文件必须是:

  • On the search $PATH在搜索$PATH
  • Specified with a fully-qualified (relative or absolute) path to the binary.指定二进制文件的完全限定(相对或绝对)路径。

So in addition to @MichaelMatsaev's (correct) suggestion to use:因此,除了@MichaelMatsaev 的(正确)建议使用:

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

The following would work as well:以下方法也可以:

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

And, while it would be a bit pathologic (ie I can't imagine you'd want to do it) for this case, you could even modify the search path inside the code and then call the binary without a fully-qualified path:而且,虽然在这种情况下这有点病态(即我无法想象你会想要这样做),但你甚至可以修改代码中的搜索路径,然后在没有完全限定路径的情况下调用二进制文件:

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

Side-note: AFAIK, there's no reason to attempt to store the os.chdir into the current variable, as os.chdir doesn't return a value.旁注:AFAIK,没有理由尝试将os.chdir存储到current变量中,因为os.chdir不返回值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM