python data read path Why use double backslash?

 

  • the python read window data path may have three representation : 

(1)'c:\\a.txt',——>Escape the way, here is a general representation \\ \ character, not prone to error
(2)r'c:\a.txt'-> string does not need to declare an escape, so here \ is an ordinary backslash character ( recommended )

(3) 'c: /a.txt' , -> directly represented by a forward slash, and as linux, no misunderstanding escaping ( recommended )

 

  • Why is there three representations of it?

Forward slash , also known as left-slash symbol is "/";

Backslash , also known as the right slash symbol is "\."

In Unix / Linux, separated by using the path forward slash "/", such as "/ home / hutaow";

In Windows, the positive backslash two can represent paths, usually see is the path separator using a backslash "\", such as "C: \ Windows \ System" .
Sometimes we see such a path written, "C: \\ Windows \\ System ", that is, with two backslash to separate path, such an approach often seen in web application or programming, in fact, above instead of: "/ Windows / System C" , you can not go wrong with this path. However, if the written "C: \ Windows \ System" , it could be all kinds of strange error appeared.


The reasons for the above problems, in this regard from the string parsing to analyze.
Learned programming people should know, as in C, when the output string, if you want to output a newline, it would have to add '\ n' this flag, similar to the output of a TAB, you add '\ t' that is, the backslash ( "\") symbol will follow in the characters following it to combine escape into other characters. According to this principle, if you want to output double quote ( ' "'), you need to enter '\"', this will be a string containing double quotes correct write memory. So if you want to enter a backslash it? Very simple, just knock '\\' on it.

See here perhaps some people have seen the looks, if: the string "C \ Windows \ System" The path string to the C compiler, actually written memory does not contain a backslash "\", even immediately after the backslash escaped with letter also become other characters, then called again bound to go wrong.
String parsing is not limited to the C compiler, the Java compiler , parsing some configuration files, Web servers , etc., will encounter this problem on the string parsing, due to the traditional Windows uses a single slash path separate form, resulting in time to parse the file path of unnecessary errors may occur, so there have been in the form of separate paths with double backslash "\\." Regardless of whether the parsing engine to parse the backslash escape character , resulting in memory is "\", the result will not be a problem.

 

Guess you like

Origin www.cnblogs.com/zwt20120701/p/11267457.html