Shell quoting as part of the standard library?

Francis Avila francisgavila at yahoo.com
Sat Dec 13 19:28:58 EST 2003


David M. Wilson wrote in message
<99dce321.0312131439.1612dee2 at posting.google.com>...
>Hello fellow users!
>
>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).

Isn't this the more general problem of escaping shell metachars in
user-supplied input?

Why don't you just let the shell deal with it?

$ cat > do_st_evil
#! /bin/sh
echo "Gotcha!"
^D
$ chmod u+x do_st_evil
$ python
Python 2.2.1 (#1, Apr 21 2002, 08:38:44)
[GCC 2.95.4 20011002 (Debian prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information
>>> import os
>>> badness = '$(./do_st_evil)'
>>> os.popen('echo '+badness).read()
'Gotcha!\n'
>>> os.environ['shellvar'] = badness
>>> os.popen('echo $shellvar').read()
'$(./do_st_evil)\n'

--
Francis Avila





More information about the Python-list mailing list