escape a string for shell commands in Python [duplicate]

十年热恋 提交于 2019-12-30 06:57:27

问题


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.


回答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
$



回答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", ...], ...)


来源:https://stackoverflow.com/questions/18116465/escape-a-string-for-shell-commands-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!