Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

escape a string for shell commands in Python [duplicate]

I'm interested to escape a string in Python3.x, such as:

SOME_MACRO(a, b)

into...

SOME_MACRO\(a,\ b\)

... so that it can be passed to a program (not gcc in this case) as a define,

eg,

some_program -DSOME_MACRO\(a,\ b\)="some expression"

I would expect shlex would have this functionality but I didn't find how to do this and checked many similar questions already.

I don't mind to write some simple function to do this, but this seems the kind of thing Python would include.

Note: the program I'm passing the argument to wont accept:

-D"SOME_MACRO(a, b)"="some expression"

... it expects the first character to be an identifier.

like image 279
ideasman42 Avatar asked Aug 08 '13 01:08

ideasman42


2 Answers

In Python 3.3 you can use shlex.quote to return a shell-escaped version of a string. It is the successor of pipes.quote, which was deprecated since Python 1.6. Note that the documentation recommends this for cases where you cannot use a list, as suggested in another answer. Also according to the documentation, the quoting is compatible with UNIX shells. I can't guarantee that it will work for your case, but a quick test with rm, using pipes because I don't have Python 3.3:

$ touch \(a\ b\)
$ ls
(a b)

>>> import subprocess, pipes
>>> filename = pipes.quote("(a b)")
>>> command = 'rm {}'.format(filename)
>>> subprocess.Popen(command, shell=True)

$ ls
$
like image 186
Paulo Almeida Avatar answered Sep 22 '22 12:09

Paulo Almeida


Doing it correctly means not having to worry about this. The shell has to worry about spaces and quotes and parens; Python does not.

proc = subprocess.Popen([..., "-DSOME_MACRO(a, b)=some expression", ...], ...)
like image 39
Ignacio Vazquez-Abrams Avatar answered Sep 22 '22 12:09

Ignacio Vazquez-Abrams