Inside a Python program I need to launch a complex command, indeed a python program that needs lots of arguments. What I need to launch is something like this:
./calculations.py -id -k "{'names':['r','pst'],'index_IN':['at','gl'],'index_OUT':[1,1]}"
-r "[False,False]" -N "[False,False]" -c "['values1**2','np.exp(values2)']"
$SCRATCH/my_dir/*
(Indeed, it's even more complex, but the main ideas are in this example).
So, the key points are the presence of both "
and '
in the example, and the use of calculation (square numbers and exponentiation with numpy
). I have tried subprocess.Popen
but somehow I can't get it to work. My attempt:
proc = subprocess.Popen(["./calculations.py -id", "-k",
"{'names':['r','pst'],'index_IN':['at','gl'],'index_OUT':[1,1]}"], ........])
Thanks.
You can save strings as raw string in python with the r prefix, if you need both ' and " than consider using a raw long python string:
cmdAttributes = r"""{'names':['r','pst'],'index_IN':['at','gl'],'index_OUT':[1,1]}"""
So same thing with flexible entries:
pnames = r"['r','pst']"
pindex_IN = r"['at','gl']"
pindex_OUT = r"[1,1]"
cmdAttributes = r"""{{'names':{Names},'index_IN':{Index_IN},'index_OUT':{Index_OUT} }}""".format(
Names = pnames,
Index_IN = pindex_IN,
Index_OUT = pindex_OUT)
cmdAttributes will be the same for both codes.