Subprocess call with arguments as python variables

Hi, I'm pretty new to Python, and I'm trying to call a subprocess from another Python Script using subprocess.call. But my arguments are variable names. So should one 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 cannot call another program by doing this. Any suggestions on what I'm doing wrong?

+4
source share
1 answer

PJust for reference. A really easy way to do something like this is to simply define the command in advance and convert it to 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)) 

Thus, it is more difficult to make mistakes when writing your team in the form of a list.

0
source

Source: https://habr.com/ru/post/1487351/


All Articles