How to execute an EXE via os.system() with spaces in the directoryname?

Fredrik Lundh fredrik at pythonware.com
Sun Dec 4 06:13:01 EST 2005


Peter Hansen wrote:

> It can't all be Windows' brain damage, since typing precisely the same
> command at the prompt (at least with the example I'm using) doesn't
> require doubling the initial quote of the command line.  Or, more
> precisely, Windows is brain damaged in at least two different places
> here, and the shell is only one of them...

the system function in the C runtime library function simply runs
the following command:

    %COMSPEC% /c command

where COMSPEC usually points to cmd.exe.

so in the normal case, it's up to cmd.exe to parse the command string.
it uses the following algorithm to decide if it should remove quotes from
the command string (for compatibility with command.com?), or if they're
better left in place:

> cmd /?

 ...

 1.  If all of the following conditions are met, then quote characters
     on the command line are preserved:

     - no /S switch
     - exactly two quote characters
     - no special characters between the two quote characters,
       where special is one of: &<>()@^|
     - there are one or more whitespace characters between the
       the two quote characters
     - the string between the two quote characters is the name
       of an executable file.

 2.  Otherwise, old behavior is to see if the first character is
     a quote character and if so, strip the leading character and
     remove the last quote character on the command line, preserving
     any text after the last quote character.

 ...

</F>






More information about the Python-list mailing list