Rawest raw string literals

eryk sun eryksun at gmail.com
Thu Apr 20 14:41:52 EDT 2017


On Thu, Apr 20, 2017 at 5:27 PM, Chris Angelico <rosuav at gmail.com> wrote:
> On Fri, Apr 21, 2017 at 2:26 AM,  <breamoreboy at gmail.com> wrote:
>> I find this:-
>>
>> s = r"ffmpeg -i  '\\server-01\D\SER_Bigl.mpg' "
>>
>> vastly superior.
>
> It's semantically different though. I don't know whether single quotes
> are valid in that context, on Windows.

On Windows, whether forward slash can be used as a path separator and
whether single quotes escape spaces and special characters depends on
the programs involved.

If you use the lpApplicationName parameter of CreateProcess (i.e.
`executable` for Popen), the system doesn't have to look at the
command line. Otherwise if the path of the executable in the command
line contains spaces, it needs to be quoted using double quotes;
single quotes have no special significance. If it has to search for
the executable, it calls SearchPath with the API's executable search
path (i.e. the directory of the calling application, the current
directory [in legacy mode], system directories, plus the PATH
environment variable) and .EXE as the optional file extension to
append.

Beyond that, the application can implement any command-line parsing
rules. In practice most programs use the CRT's argv parameter from the
`[w]main` entry point. Microsoft's CRT delimits arguments using
spaces; quotes arguments using double quotes; and escapes double
quotes using backslash [1].

If you're using the cmd.exe shell to run this, spaces and special
characters are escaped with double quotes, or with '^' outside of
quotes - except there's no way to completely escape "%" in a cmd.exe
command line (in a batch script, percent can be escaped by doubling it
as %%). If quoted, the path to the executable can use forward slash as
a path delimiter. cmd.exe implements its own custom search for the
executable, which includes the current directory (in legacy mode) and
PATH. It also tries appending all of the extensions in PATHEXT instead
of just .EXE. Since it uses the lpApplicationName parameter when it
calls CreateProcess, the system doesn't have to re-parse the command
line to locate the executable.

[1]: http://msdn.microsoft.com/en-us/library/17w5ykft.aspx


More information about the Python-list mailing list