. Advertisement .
..3..
. Advertisement .
..4..
As advised, I used some code samples in another forum, but it could not improve the problem. My question is the “oserror: [errno 8] exec format error” in python – how to solve it? The command line is:
import subprocess
Out = subprocess.Popen(['/usr/local/bin/script', 'hostname = ', 'actual server name', '-p', 'LONGLIST'],shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
and the result:
OSError: [Errno 8] Exec format error
What does the message mean? Can you advise me to fix it? If you have other better answers, leave them in the answer box below.
The cause: The error because at the top of the shell script has no shebang line.
If you run the command in the shell, there is no error. By default,
subprocess.check output()
does not run the shell; no subprocess functions run the shell unless asked clearly.Solution: You should do the ways like this:
/bin/bash
at the start of the command or the faulty file#!/bin/sh
like in a first-lineFor instance:
OSError: [Errno 8] Exec format error
can occur if you don’t have a shebang line at top of your shell script, and you try to execute the script directly. This is an example of the problem:You can fix it by adding the shebang, e.g., to a shell script; add
#!/bin/sh
to the top of your script.It executes
exit 0
with no errorsShell parses the command-line on POSIX systems. Your script will not see any spaces around
=
, e.g. ifscript
:Then, run it in the shell
produces:
Note: is not allowed around
'='
. To escape the redirection metacharacters<>
, I have added quotes around<hostname>
.Run:
Note: no
shell=True
. You don’t have to escape<>
, as no shell is running."Exec format error"
could indicate that yourscript
is not in the correct format. Run:Find out more. Compare the architecture to the output of: