Search code examples
pythonshellpopen

subprocess.call() for shell program which write a file


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


Solution

  • I used os.system() to call shell script. This do what i expected. Make sure that you have imported os module in your python code.

    invokescript( "Script1.sh" ) // Calling Function
    
    function invokescript( shfile ):  // Function Defenition
         os.system("/root/Saranya/Script1.sh")
         return;
    

    following is also executable:

    invokescript( "Script1.sh" ) // Calling Function
    
    function invokescript( shfile ):  // Function Defenition
         os.system(shfile)
         return;
    

    Thanks for your immediate response guys.