Brian
Brian

Reputation: 14846

Escaping quotation marks in python string

I'm using subprocess to call a program within python and I'm passing a string to it, which can contain quotation marks.

This is the piece of code that is giving me troubles

import subprocess
text = subprocess.Popen("""awk 'BEGIN { print "%s"}' | my_program """ % sentence, stdout=subprocess.PIPE, shell=True)

When sentence = "I'm doing this" I get the following error message

/bin/sh: -c: line 0: unexpected EOF while looking for matching `"'
/bin/sh: -c: line 1: syntax error: unexpected end of file

I guess this has to do with the way quotes are escaped in python and linux. Is there a way to fix it?

Upvotes: 0

Views: 1121

Answers (1)

Jean-François Fabre
Jean-François Fabre

Reputation: 140297

you're confusing awk and underlying shell because there's a quote in your quoted awk expression. First part is equivalent to:

awk 'BEGIN { print "I'm doing this"}'

Which is incorrect, even in pure shell.

Quickfix, escape the quotes in your sentence:

text = subprocess.Popen("""awk 'BEGIN { print "%s"}' | my_program """ % sentence.replace("'","\\'"), stdout=subprocess.PIPE, shell=True)

Proper fix: don't use awk at all just to print something, just feed input to your subprocess:

text = subprocess.Popen(my_program, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
output,error = text.communicate(sentence.encode())

(and you can get rid of the shell=True in the process)

Last point: you seem to have trouble because my_program is some program plus arguments. To pass a command such as aspell -a you can do:

my_program = "aspell -a"

or:

my_program = ['aspell','-a']

but not

my_program = ['aspell -a']

which is probably what you've done here, so Python tries to literally execute the program "aspell -a" instead of splitting into program + argument.

Upvotes: 1

Related Questions