What's the reverse of shlex.split?
We now (3.3) have a shlex.quote function. It’s none other that pipes.quote
moved and documented (code using pipes.quote
will still work). See http://bugs.python.org/issue9723 for the whole discussion.
subprocess.list2cmdline
is a private function that should not be used. It could however be moved to shlex
and made officially public. See also http://bugs.python.org/issue1724822.
How about using pipes.quote
?
import pipes
strings = ["ls", "/etc/services", "file with spaces"]
" ".join(pipes.quote(s) for s in strings)
# "ls /etc/services 'file with spaces'"
.
There is a feature request for adding shlex.join()
, which would do exactly what you ask. As of now, there does not seem any progress on it, though, mostly as it would mostly just forward to shlex.quote()
. In the bug report, a suggested implementation is mentioned:
' '.join(shlex.quote(x) for x in split_command)
See https://bugs.python.org/issue22454