[英]How to execute a file without the ".exe" extension
I have a file: "abc", it's a executable file.我有一个文件:“abc”,它是一个可执行文件。 I want to execute it by Phthon or windows CMD.
我想通过 Phthon 或 windows CMD 执行它。
If I write code:如果我写代码:
subprocess.Popen('-a -b -c', creationflags=0x08, shell=True, executable="C:\\abc")
Then, abc executed, but params(-a -b -c) been ignored然后,abc 执行,但 params(-a -b -c) 被忽略
So...How Can I reslove it?所以……我怎样才能重新爱上它?
Why not change your code to the following version为什么不将您的代码更改为以下版本
args = ['C:\\abc', '-a', '-b', '-c']
p = subprocess.Popen(' '.join(args), shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
or without shell=True
you can further reduce it to或者没有
shell=True
你可以进一步减少它
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
or you can use another method to get output from your exe and pass arguments to it或者您可以使用另一种方法从您的 exe 获取输出并将参数传递给它
Output = subprocess.check_output(' '.join(args), shell=True).decode()
在 PowerShell 中
& "C:\abc.exe"
Finally, I found the answer myself, which is to use win32process.CreateProcess
Thanks to other authors, but their answers are wrong.最后,我自己找到了答案,就是使用
win32process.CreateProcess
感谢其他作者,但他们的答案是错误的。
The correct answer is:正确答案是:
win32process.CreateProcess(r"C:\abc", "-a -b -c", None, None, 0, 0, None, None, win32process.STARTUPINFO())
The following answer does not work:以下答案不起作用:
subprocess.Popen('-a -b -c', creationflags=0x08, shell=True, executable="C:\\abc")
#or
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
Maybe I really don't know how to use subprocess
, but Windows
is really special.也许我真的不知道如何使用
subprocess
,但Windows
真的很特别。 Executable files without a suffix cannot be executed directly on the command line.没有后缀的可执行文件不能直接在命令行上执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.