This article mainly introduces the left slash in Python, right slash (forward slash and backslash) related data, very good, with reference value, the need for friends can refer to the next
">
First, the "/" left tilt is a forward slash, "\" Right tilt is a backslash, can be remembered as: Division sign is a forward slash generally for directory separators, UNIX and the web with a forward slash/,windows with backslashes, but now windows
(i) the slashes in the catalogue
Python reads the directory parameters that the file needs to enter, listing the following examples:
Path = r "C:\Windows\temp\readme.txt"
path1 = r "C:\windows\temp\readme.txt"
path2 = "C:\\windows\\temp\\readme.txt"
Path3 = "C:/windows/temp/readme.txt"
Open file functions The arguments in the open () can be either path or path1, path2, Path3.
Path: "\" is a special character in a string, and R becomes the original string without string escaping for "\ t", "\ R" in the string
Path1: Case does not affect Windows targeting to files
Path2: Use a "\" to cancel the second "\" of the special escape function, that is "\ \"
Path3: Using a forward slash to do a directory separator can also go to the corresponding directory, and the way to path3 in Python also eliminates the anti-slash \ Escape annoyance
(ii) the slashes in regular expressions
The regular expression matches the backslash "\", Why is "\\\\" or r "\ \"?
Because in the regular expression \ is a special symbol, in order to cancel its special meaning in the regular expression needs to add a \ to become the \ \, but the problem comes again, \ is a special character in the string, so we have to separate the two \ Cancel its special meaning, that is \\\\. There is a primitive string operator in Python that is used for special characters in those strings, with no escaped characters in the original string and no printing. This allows you to cancel the escape function in the string, which is r "\ \".