Python Subprocess: Unable to Escape Quotes

I know similar questions have been asked before, but they all seem to have been resolved by reworking how arguments are passed (i.e. using a list, etc).

However, I have a problem here in that I don't have that option. There is a particular command line program (I am using a Bash shell) to which I must pass a quoted string. It cannot be unquoted, it cannot have a replicated argument, it just has to be either single or double quoted.

command -flag 'foo foo1'

I cannot use command -flag foo foo1, nor can I use command -flag foo -flag foo1. I believe this is an oversight in how the command was programmed to receive input, but I have no control over it.

I am passing arguments as follows:

self.commands = [
                self.path,
                '-flag1', quoted_argument,
                '-flag2', 'test',
                ...etc...
                ]
process = subprocess.Popen(self.commands, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
results = process.communicate(input)

Where quoted_argument is something like 'foo foo1 foo2'. I have tried escaping the single quote ("\'foo foo1 foo2\'"), but I get no output.

I know this is considered bad practice because it is ambiguous to interpret, but I don't have another option. Any ideas?


The shell breaks command strings into lists. The quotes tell the shell to put multiple words into a single list item. Since you are building the list yourself, you add the words as a single item without the quotes.

These two Popen commands are equivalent

Popen("command -flag 'foo foo1'", shell=True)
Popen(["command", "-flag", "foo foo1"])