0

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?

1
  • try sentence = "I\'m doing this". you're confusing awk because there's a quote in your quoted awk expression. Commented Mar 27, 2017 at 9:34

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks so much for your answer, but I get this error when I try to replace my_program with what is should be ("aspell -a") FileNotFoundError: [Errno 2] No such file or directory: 'aspell -a'.
You want to avoid a shell and run Popen(['aspell', 'a']) instead of putting the first argument as string. See also stackoverflow.com/questions/3172470/…
Incidentally, avoiding these quoting issues is another good reason to avoid shell=True.
@tripleee actually it works when passing a string with spaces in it. Which doesn't work is passing a list of parameters but with incorrect splitting: ['aspell -a']
Yeah, with the additional caveat that I guess the last one might actually work on Windows.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.