[Tutor] path directory backslash ending

eryksun eryksun at gmail.com
Thu Apr 18 21:22:19 CEST 2013


On Thu, Apr 18, 2013 at 12:45 PM, Jim Mooney <cybervigilante at gmail.com> wrote:
> Minor question. I was adding the Py Script directory to the Win 7
> Path, and noticed that Python33 ends with a backslash but many
> directories do not. Is there a difference? Should I use backslash or
> not preferentially, or doesn't it matter at all? It does seem odd that
> there's no convention for this.

The trailing backslash shouldn't directly matter in the PATH variable,
not as far how the system splits the string on ';' and searches the
directories. It may be an issue with escaping if "%PATH%" is passed as
a quoted argument. We can test this to be sure. Create test.py:

    import sys
    print(sys.argv[1:])

Now try an example. First without quotes:

    C:\>test.py C:\Program Files;C:\Python33\
    ['C:\\Program', 'Files;C:\\Python33\\']

Obviously the C runtime (not the shell, as would be the case on a
POSIX system)  needs a little help parsing the argument string. We'll
add some quotes around it:

    C:\>test.py "C:\Program Files;C:\Python33\"
    ['C:\\Program Files;C:\\Python33"']

Ack! The trailing backslash was treated as an escape character, so we
end up with a trailing double quote. It works fine if you remove the
trailing backslash:

    C:\>test.py "C:\Program Files;C:\Python33"
    ['C:\\Program Files;C:\\Python33']

OK, so IMHO don't use a trailing backslash.

It's also a convention to not use a trailing backslash in directories
set as environment variables. For example:

    C:\>echo %ProgramFiles%
    C:\Program Files

This makes it look more natural as part of another path:

    C:\>dir /b "%ProgramFiles%\Microsoft SDKs\Windows\v7.0A"
    bin
    Include
    Lib

Note that the shell replaces %ProgramFiles% literally with C:\Program
Files, so quotes are required.


More information about the Tutor mailing list