escape a string for shell commands in Python

17,087

Solution 1

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
$

Solution 2

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", ...], ...)
Share:
17,087
ideasman42
Author by

ideasman42

Updated on June 29, 2022

Comments

  • ideasman42
    ideasman42 almost 2 years

    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.