Is this the right way to run a shell script inside Python?

后端 未结 7 443
轮回少年
轮回少年 2021-01-07 16:37
import subprocess
retcode = subprocess.call([\"/home/myuser/go.sh\", \"abc.txt\", \"xyz.txt\"])

When I run these 2 lines, will I be doing exactly t

7条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-07 17:27

    I recently ran into this problem with a script that looked like this:

    % cat /tmp/test.sh
                                  <-- Note the empty line
    #!/bin/sh
    mkdir /tmp/example
    

    The script ran fine from the command line, but failed with

    OSError: [Errno 8] Exec format error
    

    when executed via

    subprocess.Popen(['/tmp/test.sh']).communicate()
    

    (The solution, of course, was to remove the empty line).

提交回复
热议问题