Python OSError: [Errno 8] Exec 格式错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27606653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OSError: [Errno 8] Exec format error
提问by user3477108
I am having hard time parsing the arguments to subprocess.Popen. I am trying to execute a script on my Unix server. The script syntax when running on shell prompt is as follows:
/usr/local/bin/script hostname = <hostname> -p LONGLIST
. No matter how I try, the script is not running inside subprocess.Popen
我很难解析 subprocess.Popen 的参数。我正在尝试在我的 Unix 服务器上执行一个脚本。在 shell 提示符下运行时的脚本语法如下:
/usr/local/bin/script hostname = <hostname> -p LONGLIST
. 无论我如何尝试,脚本都没有在 subprocess.Popen 内运行
The space before and after "=" is mandatory.
“=”前后的空格是必须的。
import subprocess
Out = subprocess.Popen(['/usr/local/bin/script', 'hostname = ', 'actual server name', '-p', 'LONGLIST'],shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
The above does not work.
以上是行不通的。
And when I use shell=False, I get OSError: [Errno 8] Exec format error
当我使用 shell=False 时,我得到 OSError: [Errno 8] Exec format error
回答by rchang
Have you tried this?
你试过这个吗?
Out = subprocess.Popen('/usr/local/bin/script hostname = actual_server_name -p LONGLIST'.split(), shell=False,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
Edited per the apt comment from @J.F.Sebastian
根据@JFSebastian 的恰当评论进行编辑
回答by insti
If you think the space before and after "=" is mandatory, try it as separate item in the list.
如果您认为“=”前后的空格是必需的,请尝试将其作为列表中的单独项目。
Out = subprocess.Popen(['/usr/local/bin/script', 'hostname', '=', 'actual server name', '-p', 'LONGLIST'],shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
回答by jfs
OSError: [Errno 8] Exec format error
can happen if there is no shebang line at the top of the shell script and you are trying to execute the script directly. Here's an example that reproduces the issue:
OSError: [Errno 8] Exec format error
如果 shell 脚本顶部没有 shebang 行并且您尝试直接执行脚本,则可能会发生这种情况。这是一个重现该问题的示例:
>>> with open('a','w') as f: f.write('exit 0') # create the script
...
>>> import os
>>> os.chmod('a', 0b111101101) # rwxr-xr-x make it executable
>>> os.execl('./a', './a') # execute it
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/os.py", line 312, in execl
execv(file, args)
OSError: [Errno 8] Exec format error
To fix it, just add the shebang e.g., if it is a shell script; prepend #!/bin/sh
at the top of your script:
要修复它,只需添加shebang,例如,如果它是一个shell 脚本;预先考虑#!/bin/sh
在你的脚本的顶部:
>>> with open('a','w') as f: f.write('#!/bin/sh\nexit 0')
...
>>> os.execl('./a', './a')
It executes exit 0
without any errors.
它执行时exit 0
没有任何错误。
On POSIX systems, shell parses the command line i.e., your script won't see spaces around =
e.g., if script
is:
在 POSIX 系统上,shell 解析命令行,即,您的脚本不会看到周围的空格,=
例如,如果script
是:
#!/usr/bin/env python
import sys
print(sys.argv)
then running it in the shell:
然后在shell中运行它:
$ /usr/local/bin/script hostname = '<hostname>' -p LONGLIST
produces:
产生:
['/usr/local/bin/script', 'hostname', '=', '<hostname>', '-p', 'LONGLIST']
Note: no spacesaround '='
. I've added quotes around <hostname>
to escape the redirection metacharacters <>
.
注意:周围没有空格'='
。我在周围添加了引号<hostname>
以转义重定向元字符<>
。
To emulate the shell command in Python, run:
要在 Python 中模拟 shell 命令,请运行:
from subprocess import check_call
cmd = ['/usr/local/bin/script', 'hostname', '=', '<hostname>', '-p', 'LONGLIST']
check_call(cmd)
Note: no shell=True
. And you don't need to escape <>
because no shell is run.
注意:没有shell=True
。而且你不需要逃跑,<>
因为没有运行 shell。
"Exec format error"
might indicate that your script
has invalid format, run:
"Exec format error"
可能表明您的script
格式无效,请运行:
$ file /usr/local/bin/script
to find out what it is. Compare the architecture with the output of:
找出它是什么。将架构与以下输出进行比较:
$ uname -m
回答by Drachenfels
I will hiHyman this thread to point out that this error may also happen when target of Popen is not executable. Learnt it hard way when by accident I have had override a perfectly executable binary file with zip file.
我将劫持此线程以指出当 Popen 的目标不可执行时也可能发生此错误。当我偶然用 zip 文件覆盖了一个完美可执行的二进制文件时,我很难学会。
回答by Aseem Yadav
It wouldn't be wrong to mention that Pexpect
does throw a similar error
提到Pexpect
确实会引发类似的错误并没有错
#python -c "import pexpect; p=pexpect.spawn('/usr/local/ssl/bin/openssl_1.1.0f version'); p.interact()"
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/python2.7/site-packages/pexpect.py", line 430, in __init__
self._spawn (command, args)
File "/usr/lib/python2.7/site-packages/pexpect.py", line 560, in _spawn
os.execv(self.command, self.args)
OSError: [Errno 8] Exec format error
Over here, the openssl_1.1.0f
file at the specified path has exec
command specified in it and is running the actual openssl binary when called.
在这里,openssl_1.1.0f
指定路径中的文件在其中指定了exec
命令,并且在调用时正在运行实际的 openssl 二进制文件。
Usually, I wouldn't mention this unless I have the root cause, but this problem was not there earlier. Unable to find the similar problem, the closest explanation to make it work is the same as the one provided by @jfsabove.
通常,除非我有根本原因,否则我不会提及这一点,但这个问题早先不存在。找不到类似的问题,最接近的解释与上面@jfs提供的解释相同。
what worked for me is both
对我有用的是两者
- adding
/bin/bash
at the beginning of the command or file you are
facing the problem with, or - adding shebang
#!/bin/sh
as the first line.
/bin/bash
在您遇到
问题的命令或文件的开头添加 ,或- 添加shebang
#!/bin/sh
作为第一行。
for ex.
例如。
#python -c "import pexpect; p=pexpect.spawn('/bin/bash /usr/local/ssl/bin/openssl_1.1.0f version'); p.interact()"
OpenSSL 1.1.0f 25 May 2017