Eryk Sun <eryk...@gmail.com> added the comment:

As Karthikeyan noted, in a regular string literal, backslash is an escape 
character that's used in the following escape sequences:

    \N{name}   : named character
    \UXXXXXXXX : 32-bit hexadecimal ordinal (e.g. \U0010ffff)
    \uXXXX     : 16-bit hexadecimal ordinal (e.g. \uffff)
    \xXX       : 8-bit hexadecimal ordinal (e.g. \xff)
    \OOO       : 9-bit octal ordinal (e.g. \777)
    \OO        : 6-bit octal ordinal (e.g. \77)
    \O         : 3-bit octal ordinal (e.g. \7)

    \a : \x07, \N{BEL}, \N{ALERT}
    \b : \x08, \N{BS}, \N{BACKSPACE}
    \t : \x09, \N{HT}, \N{TAB}, \N{CHARACTER TABULATION}, \N{HORIZONTAL 
TABULATION}
    \n : \x0a, \N{LF}, \N{NL}, \N{LINE FEED}, \N{NEW LINE}
    \v : \x0b, \N{VT}, \N{LINE TABULATION}, \N{VERTICAL TABULATION}
    \f : \x0c, \N{FF}, \N{FORM FEED}
    \r : \x0d, \N{CR}, \N{CARRIAGE RETURN}
    \" : \x22, \N{QUOTATION MARK}
    \' : \x27, \N{APOSTROPHE}
    \\ : \x5c, \N{REVERSE SOLIDUS}

For a Windows path, either we can use a normal string literal with backslash 
path separators escaped by doubling them or we can use a raw string literal. 

One corner case with a raw string literal is that it can't end with an odd 
number of backslashes. We can address this in one of two ways. Either rely on 
the compiler's implicit concatenation of string literals, or rely on the 
system's path normalization to collapse multiple path separators (except at the 
beginning of a path). For example:

    >>> print(r'C:\Users' '\\')
    C:\Users\
    >>> print(r'C:\Users\\')
    C:\Users\\

The system normalizes the second case to collapse repeated backslashes. For 
example:

    >>> print(os.path._getfullpathname(r'C:\Users\\'))
    C:\Users\
    >>> os.path.samefile(r'C:\Users\\', r'C:\Users' '\\')
    True

We can also use forward slash as the path separator for file-system paths (but 
not registry paths), such as paths that we're passing to open() or os 
functions. I don't recommend this if a file-system path is to be passed as a 
command-line argument. Some programs use forward slash as a switch for 
command-line options. In this case first normalize the path via 
os.path.normpath, or via replace('/', '\\').

In some cases a path may be returned to us in Windows with a "\\?\" prefix 
(backslash only), which is sometimes referred to as an extended path. (More 
specifically, it's a native path in the device namespace.) This tells the 
Windows API to skip path normalization. If a path begins with exactly this 
prefix, then appending components to it with forward slash results in a path 
that will not work. Use os.path.join, or normalize the path via 
os.path.normpath to ensure the final path uses only backslash as the path 
separator.

----------
nosy: +eryksun

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue37939>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to