Why does Python read absolute paths with a forward slash?

Source: Internet
Author: User
The Windows directory is a backslash (\) that separates subdirectories
In Python or R, you need to use a forward slash (/) to represent
Is there any reason for this?

Reply content:

Because backslash is astray. You may have to say that Windows is earlier than UNIX to enter the PC, and backslash's origins are about the same as UNIX, why backslash is astray? Because if you cross in the end, and even Unix basic C language also do not use, or you write backslash every time to be written "\\". Reading a file in Windows can be used \, but inside the string \ is used as an escape character, then there are two ways of describing the path

' C:\\a.txt ', escaping the way
R ' c:\a.txt ', declaration string does not need to be escaped

This implements the use \ to access the path.


and Linux under the path is used/to split, the same description of the path there are two ways, always uncomfortable.


Python provides compatibility processing so that Linux style paths are available under Windows, so many times everyone will choose the Linux style. Such a simple script can be run on two platforms.

Python has a library called Ntpath for Windows path processing.

Windows uses \ partition path is because the DOS era will/as a parameter guide use, lazy someone do not want to take the time to deal with the relevant analysis, so in order to rush rough use of \ substitution.

Windows can both, the string before the R to prevent the change of meaning. Like R ' c:/***/** '
Linux only uses '/'
If you're not sure, you can use OS.PATH.SEP to combine paths, with a variety of delimiters (SEP,LINESEP) in the standard library OS, and so on across platforms. See the documentation for details

Say a few more words,
This type of IO problem standard library is used in the book of Python Primer, and the best way is not to write a few lines of code to try it?

In fact, Windows is both positive and negative, but Python picked one of them because the original target operating system of open source language is generally linux/unix, Unix with/do path delimited saying, Windows mainly use is "backslash (backslash)"? Because the forward slash "/" is used by Win/dos to make command-line parameter separators (such as "/?"). So the directory delimiter can only be used anti-normal anti-slash "\". Otherwise, the program for the command line "/ABC" to resolve the parameter or root directory under the file/directory is at a loss. UNIX is a good choice here, using the horizontal line "-" prefix to denote command row parameters, such as "-H", and then take it for granted that the normal use of regular slash "/" to partition the directory. DOS inside the command "dir/a" "format c:/q", the parameters of the DOS command is to use the forward slash "/", so there is no way, win3.1 and Win95 are to first install DOS6.22 to use Ah, so DOS is used forward slash "/", not windows. Also worry about the pros and cons, transplant sex? It seems that the python-provided cross-platform feature is white. I've seen a string that writes eight backslashes in a ╮(╯▽╰)╭.

How nice to use a slash (^o^)/~
  • Related Article

    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.