gpt4 book ai didi

python - 使用 shebang 通过 subprocess.call 执行 python 脚本

转载 作者:太空宇宙 更新时间:2023-11-03 13:23:19 24 4
gpt4 key购买 nike

我正在用 Python 3 编写一个(有点)模块化应用程序,我想从中运行任意程序,所述程序在运行时指定,不一定是 python 脚本。

所以我用例如,

subprocess.call([spam, "-i", eggs, "-o", ham])

如果 spam 是一个 python 脚本,具有 shebang 到 python3 和可执行权限,我得到

OSError: [Errno 8] Exec format error

如果我

subprocess.call(["python3", spam, "-i", eggs, "-o", ham])

它工作正常。

你知道为什么吗?如何在不指定 python3 的情况下运行 spam

最佳答案

您需要使用 shell=True,并且您需要将数组转换为命令字符串,如下所示:

subprocess.call(' '.join([spam, "-i", eggs, "-o", ham]), shell=True)

这将调用 shell 而不是直接命令,并且 shell 应该能够处理 shebang。

关于python - 使用 shebang 通过 subprocess.call 执行 python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6441507/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com