Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/The oserror: [errno 8] exec format error error: What should you do?
Next
Answered
Julia Bataille
  • 18
Julia Bataille
Asked: May 17, 20222022-05-17T13:00:27+00:00 2022-05-17T13:00:27+00:00In: python

The oserror: [errno 8] exec format error error: What should you do?

  • 18

. 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.

oserror
  • 2 2 Answers
  • 1k Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    dttutoria Expert
    2022-06-05T16:48:47+00:00Added an answer on June 5, 2022 at 4:48 pm

    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:

    • Adding /bin/bash at the start of the command or the faulty file
    • Adding shebang #!/bin/sh like in a first-line

    For instance:

    #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
    • 23
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Rose Albert
    2022-05-25T19:58:38+00:00Added an answer on May 25, 2022 at 7:58 pm

    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:

    >>> 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

    You can fix it by adding the shebang, e.g., to a shell script; add #!/bin/sh to the top of your script.

    >>> with open('a','w') as f: f.write('#!/bin/sh\nexit 0')
    ... 
    >>> os.execl('./a', './a')

    It executes exit 0 with no errors

    Shell parses the command-line on POSIX systems. Your script will not see any spaces around =, e.g. if script:

    #!/usr/bin/env python
    import sys
    print(sys.argv)

    Then, run it in the shell

    $ /usr/local/bin/script hostname = '<hostname>' -p LONGLIST

    produces:

    ['/usr/local/bin/script', 'hostname', '=', '<hostname>', '-p', 'LONGLIST']

    Note: is not allowed around '='. To escape the redirection metacharacters <>, I have added quotes around <hostname>.

    Run:

    from subprocess import check_call
    
    cmd = ['/usr/local/bin/script', 'hostname', '=', '<hostname>', '-p', 'LONGLIST']
    check_call(cmd)

    Note: no shell=True. You don’t have to escape <>, as no shell is running.

    "Exec format error" could indicate that your script is not in the correct format. Run:

    $ file /usr/local/bin/script

    Find out more. Compare the architecture to the output of:

    $ uname -m
    • 9
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.