subprocess.Popen(): OSError: [Errno 8] Exec format error in python?

前端 未结 7 2111
余生分开走
余生分开走 2020-12-06 09:07

Yesterday, I wrote and ran a python script which executes a shell using subprocess.Popen(command.split()) where command is string whic

7条回答
  •  误落风尘
    2020-12-06 09:37

    The error is because the executables are not given in the prescribed format for subprocess to execute it.

    example:

    shell_command1 = r"/path/to/executable/shell/file"
    
    shell_command2 = r"./path/to/executable/shell/file"
    

    subprocess.call(shell_command1) or subprocess.Popen(shell_command1) will not be able to run shell_command1 because subprocess needs an executable command like (mailx, ls, ping, etc.) or executable script like shell_command2 or you need to specify command like this

    subprocess.call(["sh", shell_command1])
    subprocess.Popen(["sh", shell_command1])
    

    but however, you can use os.system() or os.Popen() as well

提交回复
热议问题