cancel
Showing results for 
Search instead for 
Did you mean: 

Calling another script using subprocess.call method in python script

abhinav
New Contributor

Hi Team,
we have requirement to call script from current python script.to call another script i’m using subprocess.call method.

subprocess.call(r"C:\Python27\python.exe" + " " + r"C:\Test\Scripts\Test.py" , shell=True)

but i’m not receiving any error nor any result and pipeline goes on executing continuously without giving any result or error.

python code :-

Import the interface required by the Script snap.

from com.snaplogic.scripting.language import ScriptHook
import java.util
import subprocess

class TransformScript(ScriptHook):
def init(self, input, output, error, log):
self.input = input
self.output = output
self.error = error
self.log = log

# The "execute()" method is called once when the pipeline is started
# and allowed to process its inputs or just send data to its outputs.
def execute(self):
    self.log.info("Executing Transform script")
    while self.input.hasNext():
        try:
            # Read the next document, wrap it in a map and write out the wrapper
            in_doc = self.input.next()
            record = in_doc

            subprocess.call(r"C:\\Python27\\python.exe" + " " + r"C:\\Test\\Scripts\\Test.py"  , shell=True)
			#subprocess.call("C:\\Python27\\python.exe" + " " + "C:\\Test\\Scripts\\Test.py"  , shell=True)
            self.output.write(record)
        except Exception as e:
            errWrapper = {
                'errMsg' : str(e.args)
            }
            self.log.error("Error in script")
            self.error.write(errWrapper)

    self.log.info("Finished executing the Transform script")

The Script Snap will look for a ScriptHook object in the “hook”

variable. The snap will then call the hook’s “execute” method.

hook = TransformScript(input, output, error, log)

can someone suggest what i’m doing wrong.

1 REPLY 1

tlikarish
Employee
Employee

A couple tips you could try from looking at your script.

The first step I’d take in debugging is checking the output of the subprocess call method. You can do this using the check_output method. This will give you more information about whether the output succeeded or not and also output from executing.

Also, the call method takes a list where the first element is the command and subsequent elements are the arguments. So you could try:

subprocess.call(["C:\\Python27\\python.exe", "C:\\Test\\Scripts\\Test.py"], shell=True)

Another option for building paths that’s OS independent is to use os.path.join. That should give a valid path to the file without having to escape backslashes and all that.

Hope this helps and good luck!