What's the Scoop on \\ for Paths? (Win)
MRAB
python at mrabarnett.plus.com
Sun Jan 31 15:02:00 EST 2010
W. eWatson wrote:
> Steve Holden wrote:
>
>> You need to read up on string literals is all. "\\" is simply the
>> literal representation of a string containing a single backslash. This
>> comes about because string literals are allowed to contain special
>> "escape sequences" which are introduced by a backslash; since this gives
>> the backslash a special meaning in string literals we also have to use
>> an escape sequence ("\\") to represent a backslash.
>>
>> In practice you will find that
>>
>> a) Many Windows APIs (but not the command line) are just as happy with a
>> forward slash as a backslash to separate file path components; and
>>
>> b) The best practice is to build filenames using the routines provided
>> in the os.path module, which guarantees to give results correct for the
>> current platform.
>>
>> regards
>> Steve
> Basic sys functions brought out the \ separator for paths.
>
> What am I missing here? Looks OK to me.
>
>
> >>> abc=r'xyz\\'
> >>> abc
> 'xyz\\\\'
> >>> print abc
> xyz\\
> >>> abc.replace(r'\',r'z')
>
> SyntaxError: invalid syntax
> >>> abc
> 'xyz\\\\'
It's not possible to end a raw string with a single backslash, and if
you end it with a double backslash then you'll get a double backslash.
Very annoying.
More information about the Python-list
mailing list