【问题标题】:Execute externall compiled program in Python在 Python 中执行外部编译的程序
【发布时间】:2023-03-20 21:16:02
【问题描述】:

我想在 Python 中执行一个外部编译程序。在外壳中,

./bin/myProgram ./dir1/arg1 ./dir/arg2 arg3 arg3 arg4

我的 Python 脚本如下所示:

import subprocess
subprocess.call(["./Users/Solar/Desktop/parent/child1/child2/bin/myProgram",
                 "/Users/Solar/Desktop/parent/child1/child2/dir1/arg1",
                 "/Users/Solar/Desktop/parent/child1/child2/dir1/arg2",
                 "arg3", "arg4", "arg5"])

但我收到此错误:

Traceback (most recent call last):
  File "/Users/Solar/Desktop/test.py", line 5, in <module>
    "32", "0.06", "15"])
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 522, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in __init__
    errread, errwrite)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1335, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

Process finished with exit code 1

你能帮帮我吗?提前谢谢你!

【问题讨论】:

  • OSError: [Errno 2] No such file or directory。显然,你的路径是错误的。

标签: python shell subprocess


【解决方案1】:

解决了,我只好执行了

subprocess.call(["/Users/Solar/Desktop/parent/child1/child2/bin/myProgram",
                 "/Users/Solar/Desktop/parent/child1/child2/dir1/arg1",

...

代替

subprocess.call(["./Users/Solar/Desktop/parent/child1/child2/bin/myProgram",
                 "/Users/Solar/Desktop/parent/child1/child2/dir1/arg1",
...

谢谢大家!!

【讨论】:

    【解决方案2】:

    subprocess.Popen 问题时

    OSError: [Errno 2] No such file or directory
    

    那只是因为找不到可执行文件(如果找不到文件参数之一,则报告错误的是子进程,并且不会引发异常)

    考虑到/Users/ 是有效的(针对当前目录的根目录),在您的情况下./Users/... 可能无效,所以这是您的问题。

    "./Users/Solar/Desktop/parent/child1/child2/bin/myProgram" 应该是"/Users/Solar/Desktop/parent/child1/child2/bin/myProgram"

    注意:更好的编码可以避免错误:

    import subprocess
    root = "/Users/Solar/Desktop/parent/child1/child2"
    subprocess.call([os.path.join(root,"bin/myProgram"),
                     os.path.join(root,"dir1/arg1"),
                     os.path.join(root,"dir1/arg2"),
                     "arg3", "arg4", "arg5"])
    

    【讨论】:

    • 是的!谢谢亲爱的琼!
    猜你喜欢
    • 2018-06-03
    • 2010-12-21
    • 1970-01-01
    • 2012-12-09
    • 1970-01-01
    • 2015-02-21
    • 2012-09-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多