Why must a forward slash be used to read an absolute path in Python?

Source: Internet
Author: User
Windows directories all use reverse bars () to separate sub-directories. When Python or R is used, positive bars () are used to indicate the reason? Windows directories are separated by backslashes (\).
In Python or R, you need to use a forward slash (/) to represent
Is there any reason for this? Reply content: Because backslash is an evil path. You may want to say that Windows is earlier than Unix in PC, and the background of backslash is about as long as Unix. Why is backslash a bad path? Because if you want to be horizontal, simply do not use the Unix-based C language. Otherwise, every time you write backslash, you must write it 「\\」. \ Can be used to read files in windows, but \ is used as an escape character in the string. There are two methods to describe the path.

'C: \ a.txt ', escape Method
R'c: \ a.txt ', which declares that the string does not need to be escaped.

In this way, \ is used to access the path.


In Linux, the path is separated by a slash (/). It is always uncomfortable to describe the path in two ways.


Python provides compatible processing to make the Linux style path available in windows, so many times you will choose the Linux style. Such a simple script can be run on both platforms.

Python has a library named ntpath for processing windows paths.

In windows, \ is used to split the path because/was used as a parameter in the DOS era. a lazy person did not want to take the time to process related parsing, so he used \ instead in a hurry.

Windows supports both, and r is added before the string to prevent conversion. For example, r'c :/***/**'
Linux only uses '/'
If you are not sure, you don't need to worry about such details. You can use OS. path. sep to combine paths. The standard library OS provides various separators (sep and linesep) for cross-platform use. For more information, see the document.

Say a few more words,
I/O problems of this type are included in the usage of the standard library. Is it best to write a few lines of code by yourself?

In fact, windows is both positive and negative, but python chooses one of them because the initial target operating system of the open-source language is generally Linux/Unix, and Unix is separated by/as a path, does Windows mainly use Backslash? The forward slash "/" is used by Win/dos to separate command line parameters (for example, "/?"). Therefore, the directory separator can only use the Backslash "\" that the anti-normal person is used. Otherwise, the program will not be able to parse "/abc" in the command line as a parameter or a file/directory in the root directory. Unix has better options here, and uses the hyphen "-" prefix to represent command line parameters, such as "-h ", then, we naturally use the forward slash "/" that meets the habits of normal people to separate directories. In DOS, the command "dir/a" "format c:/q" is used. The DOS command parameter uses the forward slash "/", so there is no way to do this, both win3.1 and win95 must be installed with DOS6.22 first, so the forward slash "/" is used in DOS, not in windows. What about forward and backward slashes and portability? It seems that the cross-platform feature provided by python is white. We have seen the delimiter (backslash) delimiter that writes eight backslash characters in the string.

How nice to use a slash \ (^ o ^ )/~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.