4
votes

Hi I am quite new to Python and I am trying to call a subprocess from another Python Script using subprocess.call. But my arguments are variable names. So, should I use subprocess.call or subprocess.popen ?

I want to execute the following command from another python script:

python npp.python -i fname -o fname+"out" -l fname+"log" -e excplist -i ignorelist 

So, should I do

subprocess.Popen(['python', 'npp.python', '-i', fname , 'o', fname+"out", '-l', fname+"log", '-e', excplist,'-i',ignrlist]).communicate()

I am not able to call the other program by doing so. Any suggestions on what I am doing wrong ?

1
You have the right idea, but are missing a - on the -o option. - Martijn Pieters
subprocess.call is basically subprocess.Popen, but it returns the exit code. Use subprocess.Popen. Also, can you say what's going wrong? Why is the code not working? - kirbyfan64sos
what about sh the python third module ? - Dreampuf

1 Answers

0
votes

PJust for reference. A really simple way to do something like this is simply defining the command beforehand and than transforming it into a list of arguments.

command = "python npp.python -i {file} -o {file}.out -l {file}.log -e {excep} -i {ignore}".format(file=pipe.quote(fname), excep=exceptlist, ignore=ignorelist)

subprocess.call(shlex.split(command)) # shlex.split is safer for shell commands than the usual split
# or popen if the return code isn't needed
subprocess.Popen(shlex.split(command))

This way it is harder to make mistakes when writing your command in list form.