简体   繁体   中英

subprocess with double quotations

In python have to run the following perl command with subprocess.

perl command:

perl dcm.pl -r ona_sql sql="select something from table where name='somename'" header=no

I need to escape the quotations, but can't get it to work.

The following example would make the most sense for me, but it doesn't work either.

import subprocess
import sys

subprocess.check_output(["/usr/bin/perl", "/opt/ona/bin/dcm.pl", "-r", "ona_sql", "sql=\"select something from table where name='somename'\"", "header=no"]).decode(sys.stdout.encoding)

results in:

subprocess.CalledProcessError: Command '['/usr/bin/perl', '/opt/ona/bin/dcm.pl', '-r', 'ona_sql', 'sql="select something from table where name=\'somename\'"', 'header=no']' returned non-zero exit status 3

You can use triple quotes to enclose string literals with both single quotes and double quotes:

subprocess.check_output(['perl', '/opt/ona/bin/dcm.pl', '-r', 'ona_sql', '''sql="select something from table where name='somename'"''', 'header=no'])

Try using single quotes instead:

'sql="select something from table where name=\'somename\'"'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM