[Tutor] subprocess.call list vs. str argument

eryksun eryksun at gmail.com
Tue Feb 25 23:30:28 CET 2014


On Tue, Feb 25, 2014 at 4:54 PM, Dave Angel <davea at davea.name> wrote:
> CreateProcess has its own design bobbles as well. For example,  if
> you forget to put quotes around the program name,  it will
> happily try to add ".exe" to *multiple* places in the hopes that
> one of them will work.
>
> Adding a file c:\program.exe to a system will blow up lots of
> programs that were working by mistake for years.

Yes, using a string requires quoting the path for the executable if
you aren't using lpApplicationName (i.e. Popen's "executable"). I
noticed for shell=True they aren't using list2cmdline for COMPSPEC;
they just trust it. So I wrote the following test to exemplify the
problem you're talking about:

    >>> import os, subprocess
    >>> open('temp.c', 'w').write(r'''
    ... #include <stdio.h>
    ... int main(int argc, char *argv[]) {
    ...     printf("\n\n*** spam! ***\n\n");
    ...     return 0;
    ... }''')
    107
    >>> os.system('cl temp.c /FeC:\\Program.exe 1>nul 2>&1')
    0
    >>> os.environ['COMSPEC'] = r'C:\Program Files\doesnt\matter'
    >>> p = subprocess.Popen('dir', shell=True)
    >>>

    *** spam! ***


More information about the Tutor mailing list