os.path and Windows UNC paths

Matt Gerrans mgerrans at mindspring.com
Thu Jul 11 02:09:27 EDT 2002


> the trailing \ is escaping the ending ' of the r'...' string. I just ran
> across the caution about having odd numbers of \ at the end of an r'...'
> string last evening.

I noticed this a while back.   It doesn't seem to make sense.   I thought
nothing inside a raw was escaped, so why is the last character?    I suppose
you can resort to stuff like this:
   path = os.sep.join(('','','server','share',''))
or this:
   path = r'\\server\share' + '\\'
or just give up and do the old:
   path = '\\\\server\\share\\'
but I thought the idea behind the raw strings was to save a little typing
and make code a bit more readable since, as in c/c++ you can do everything
with cooked strings, it is just more tedious and ugly.

I see in the Python doc where it says
"""
r"\" is not a value [sic. -- I think he intended "valid"] string literal
(even a raw string cannot end in an odd number of backslashes).
Specifically, a raw string cannot end in a single backslash (since the
backslash would escape the following quote character).
"""
I don't understand why this is the case, though.   Is a raw string really
treated as a standard string until some later time when it is translated to
raw?





More information about the Python-list mailing list