Executing python scripts with subprocess.call using shebang

醉酒当歌 提交于 2020-01-21 12:54:12

问题


I'm writing a (somewhat) modular application in Python 3 and I'd like to run arbitrary programs from it, said program being specified at runtime and not necessarily a python script.

So I use for example,

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

If spam is a python script, with shebang to python3 and executable rights, I get

OSError: [Errno 8] Exec format error

if I

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

it works fine.

Do you know why? How can I run spam without specifying python3?


回答1:


You need to use shell=True, and you need your array to be turned into a command string, like this:

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

This will invoke the shell instead of the direct command, and the shell should be able to handle the shebang.




回答2:


Try

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


来源:https://stackoverflow.com/questions/6441507/executing-python-scripts-with-subprocess-call-using-shebang

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!