Shell quoting as part of the standard library?

Peter Otten __peter__ at web.de
Sat Dec 13 18:35:24 EST 2003


David M. Wilson wrote:

> I've been using Python in a couple of different environments for a few
> years now. I have quite often found that I have needed to deal with
> filenames which may contain characters that confuse the UNIX shell
> (space and double quote in particular).
> 
> Quite a while ago I came up with two simple functions which allowed me
> to safely build a shell command line quickly. I've used the functions
> hundreds of times by now, and find myself constantly cut/pasting them
> into new projects. It is very likely that someone other than myself
> would find these functions to be useful, and I was wondering if such a
> thing might find a home in the standard library.
> 
>    http://botanicus.net/dw/tmp/shellquote.py
> 
> Does this sort of thing have a home in the standard library? It
> doesn't really apply across platforms, which is my only real concern.
> Where should it be added to? shutil?

>>> from commands import mkarg
>>> mkarg("one silver $")
" 'one silver $'"
>>> mkarg("one's own $")
' "one\'s own \\$"'

So something very similar for argument preparation is already there. The
joining, as far as I know, currently has to be done manually:

>>> "".join([mkarg(a) for a in ["a a", "$'"]])
' \'a a\' "\\$\'"'

Peter




More information about the Python-list mailing list