On Thu, Sep 20, 2012 at 6:25 AM, eryksun <[email protected]> wrote:
>
> I forgot you need to escape special characters in the arguments. You
> can add quoting and escape special characters at the same time with
> the undocumented function pipes.quote:
>
> import pipes
>
> args = tuple(pipes.quote(arg) for arg in (arg1, arg2, arg3))
> cmd = 'python test.py %s %s %s' % args
FYI, here's what pipes.quote does. If the string has any special
characters, it first replaces any single quotes in the string with
'"'"' and then wraps the string in single quotes. For example
"abc'def" becomes "'abc'\"'\"'def'". The shell doesn't use special
characters ($`\) in single-quoted strings.
Source:
def quote(file):
"""Return a shell-escaped version of the file string."""
for c in file:
if c not in _safechars:
break
else:
if not file:
return "''"
return file
# use single quotes, and put single quotes into double quotes
# the string $'b is then quoted as '$'"'"'b'
return "'" + file.replace("'", "'\"'\"'") + "'"
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor