Call command-line oriented script from another python script -


i using script written in python uses argparse module it's arguments command line. try modify file less possible various people work on it.

ex: script called clscript.py , call

python clscript.py -option1 arg1 -flag1 -option2 arg2 

but i'm facing case automate things 1 level higher , automatically launch script wide range of script-generated arguments.

i keep using existing organisation of options , flags available in script.

for example, when run clscript.py toplevelscript.py through :

subprocess.call("python clscript.py -option1 arg1 -flag1 -option2 arg2") 

,and see output going wrong, stop execution toplevelscript.py, clscript.py continue run independently in python process have kill manually. cannot neither launch toplevelscript.py in debug mode stop @ breakpoint in clscript.py.

i inside python, without building command line string , launching clscript.py subprocess. every call remains attached same original launch, function call, , not creating multiple python threads subprocess.call() .

something passing list of string options, flags , arguments somehow script maybe?

is there like

import clscript.py clsimulator(clscript,["-option1",arg1,"-flag1","-option2",arg2]) 

first of all, use http://docs.python.org/2/library/subprocess.html#subprocess.popen , not subprocess.call():

import subprocess  child = subprocess.popen(     ['python', 'clscript.py', '-option1', 'arg1', '-flag1', '-option2', 'arg2'],     stdin=subprocess.pipe,     stdout=subprocess.pipe,     stderr=subprocess.pipe ) 

please notice first argument pass array of strings want.
secondly redirection of standard file descriptors important. see http://docs.python.org/2/library/subprocess.html#subprocess.pipe.
have child variable holds instance of popen class.
can instance?

# check if child terminated , possibly returncode child.poll() # write child's standard input file-like object accessible child.stdin # read child's standard output , standard error file-like objects accesible child.stdout child.stderr 

you said wanted detect if goes wrong in child process it's output.
don't find stdout , stderr quite useful case?
wanted terminate child if detected went wrong.

child.kill() child.terminate() child.send_signal(signal) 

if in end sure went want let child finalize normally, should use

child.wait() 

or better

child.communicate() 

because communicate handle lots of output properly.

good luck!


Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -