【问题标题】:How to execute a file without the ".exe" extension如何执行没有“.exe”扩展名的文件
【发布时间】:2021-11-30 20:43:40
【问题描述】:

我有一个文件:“abc”,它是一个可执行文件。 我想通过 Phthon 或 windows CMD 执行。

如果我写代码:

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

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

那么...我该如何重新爱上它?

【问题讨论】:

  • 你试过subprocess.Popen("C://abc -a -b -c", shell=True)吗?见stackoverflow.com/questions/11284147/…
  • 这应该可以。首选方法是使用参数列表(并删除 shell=True)。你怎么能确定/证明这些论点被忽略了?例如这个工作:p=subprocess.Popen("/c dir",executable=r"C:\Windows\System32\cmd.exe")

标签: python powershell cmd executable


【解决方案1】:

为什么不将您的代码更改为以下版本

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()

【讨论】:

  • 如果可执行文件(c:\\abc) 文件名没有“.exe”,Popen 不起作用:abc 不会被调用。当我将 abc 重命名为 abc.exe 时,它​​可以工作
【解决方案2】:

在 PowerShell 中

& "C:\abc.exe"

【讨论】:

    【解决方案3】:

    最后我自己找到了答案,就是使用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真的很特别。没有后缀的可执行文件不能直接在命令行执行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-04
      • 1970-01-01
      • 2015-03-31
      • 1970-01-01
      • 2021-01-13
      • 1970-01-01
      • 2010-10-25
      • 2016-12-24
      相关资源
      最近更新 更多