Reputation: 662
I am trying to run a shell command from python, and getting syntax errors. It is probably related to the fact that there are nested quotes, but I can't figure out how to make it work.
Here is the command:
In [44]: command
Out[44]: '/Applications/itt/idl/bin/idl -e "print, barycorr(2456718.886512, 16.109814, -36.799472, 0, obsname=\'CTIO\')"'
In [45]: print command
/Applications/itt/idl/bin/idl -e "print, barycorr(2456718.886512, 16.109814, -36.799472, 0, obsname='CTIO')"
When I run the command using either of the two below, I get a syntax error in idl:
subprocess.call(command, shell=True)
subprocess.call(shlex.split(command))
If I run the command (output of [45]) from the command line, it works perfectly. Any idea what I am doing wrong?
Thanks!
Upvotes: 2
Views: 2228
Reputation: 11614
Try to construct your list manually, eg.:
cmd_lst = ['/Applications/itt/idl/bin/idl',
'-e',
("print, barycorr(2456718.886512, 16.109814, -36.799472, 0,"
" obsname='CTIO')"),
]
subprocess.call(cmd_lst)
I splitted the long string into two lines. The parenthesis makes sure it's handled as the same string even if spanning over multiple lines. As a side-effect the escapes for the single quotes may be omitted if not needed by the called program.
Upvotes: 1