[Tutor] Passing arguments to & running a python script on a remote machine from a python script on local machine .

eryksun eryksun at gmail.com
Thu Sep 20 14:19:33 CEST 2012


On Thu, Sep 20, 2012 at 6:25 AM, eryksun <eryksun at gmail.com> 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("'", "'\"'\"'") + "'"


More information about the Tutor mailing list