On Windows, how do I protect arguments to shell scripts launched with subprocess?

arve.knudsen at gmail.com arve.knudsen at gmail.com
Tue Feb 8 07:34:31 EST 2011


Since upgrading to Python 2.7, I've run into the problem that when I
launch shell scripts (.e.g, *.bat) via subprocess.Popen (with False
for the 'shell' option, mind you), the arguments get interpreted by
the shell. For instance, the '|' character, no longer gets passed
verbatim to the script. What is now the correct way to protect
arguments passed as a list to subprocess.Popen? I tried enclosing each
argument in double quotes, but subprocess in turn thwarts my attempt,
by protecting each double quote with a backslash! For example, if I
were to pass ['"|"'] as the argument list to subprocess.Popen, it'd be
transformed like this:
>>> subprocess.list2cmdline(['"|"'])


More information about the Python-list mailing list