【问题标题】:Why subprocess.call () is showing error in linux?为什么 subprocess.call() 在 linux 中显示错误?
【发布时间】:2016-06-02 00:01:30
【问题描述】:

让我们考虑一下我需要执行一个名为 smart.exe 的程序的 Linux 平台,该程序使用 input.dat 文件。两个文件都放在同一个目录下,每个文件的文件权限都是777。

现在,如果我在终端窗口中运行以下命令,smart.exe 将完全执行,没有任何错误。

$./smart.exe input.dat

另一方面,如果我使用位于同一目录中的名为 my_script.py 的以下 python 脚本,则会出现错误。

my_script.py 有以下代码:

#!/usr/bin/python
import os, subprocess

exit_code = subprocess.call("./smart.exe input.dat", shell = False)

错误如下:

File "my_script.py", line 4, in <module>
exit_code = subprocess.call("./smart.exe input.dat", shell = False) 
File "/usr/lib64/python2.6/subprocess.py", line 478, in call

p = Popen(*popenargs, **kwargs)
File "/usr/lib64/python2.6/subprocess.py", line 642, in __init__
    errread, errwrite)
File "/usr/lib64/python2.6/subprocess.py", line 1234, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory

谁能告诉我为什么会这样。请注意 smart.exe 大约需要 10 秒才能完全完成。这可能是问题的线索。

还请告知是否有任何其他方式可以从 my_script.py 运行 smart.exe。非常感谢您的解决方案!

【问题讨论】:

  • 你打算如何在 Linux 机器上运行 Windows 二进制文件?你在使用 WINE 之类的东西吗?

标签: python linux python-2.7


【解决方案1】:

您应该决定是否需要 shell 支持。

如果你想使用 shell(这里没有必要),你应该使用exit_code = subprocess.call("./smart.exe input.dat", shell=True)。然后 shell 解释你的命令行。

如果您不想要它(因为您不需要它并希望避免不必要的复杂性),您应该使用exit_code = subprocess.call(["./smart.exe", "input.dat"], shell=False)

(在 Linux 下命名你的二进制文件 .exe 是没有意义的。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-06
    • 2022-06-13
    • 1970-01-01
    • 2019-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多