subprocess.call() for shell program which write a file
Question: I need to execute a shell script using python. Output of shell program is a
text file. No inputs to the script. Help me to resolve this.

    
    
    def invokescript( shfile ):
      s=subprocess.Popen(["./Script1.sh"],stderr=subprocess.PIPE,stdin=subprocess.PIPE);
      return;
    
    invokescript("Script1.sh");
    

On using above code., I receive the following error.

    
    
    Traceback (most recent call last):
      File "./test4.py", line 12, in <module>
        invokescript("Script1.sh");
      File "./test4.py", line 8, in invokescript
        s=subprocess.Popen(["./Script1.sh"],stderr=subprocess.PIPE,stdin=subprocess.PIPE);
      File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
        errread, errwrite)
      File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child
        raise child_exception
    OSError: [Errno 8] Exec format error
    

Thanks in advance...


Answer: Try this:

    
    
    import shlex
    
    def invokescript(shfile):
        return subprocess.Popen(
            shlex.split(shfile),
            stderr=subprocess.PIPE,
            stdin=subprocess.PIPE
        )
    
    invokescript("Script1.sh");
    

And add `#!/usr/bin/env bash` to your bash file of course.

