Python represents a file path in a Windows system

Source: Internet
Author: User

In a Windows system, the path uses \. While the Linux system, the path uses/. \ is also an escape character, so there is a problem when using \.

If the luck is good, \ can not be escaped after the character, or can output normally:

Print ("C:\Program files\windows Media Player\wmplayer.exe")

The following is an escaped condition:

Print ("C:\Windows\notepad.exe")

To get the file path normally, you have to process the string.

Method One: The escape character \ \ means \:

Print ("C:\\windows\\notepad.exe")

This makes it cumbersome to process strings.

Method Two: string preceded by R or R declaration string do not escape:

Print (R "C:\Windows\notepad.exe")

Method Three: Python can also use/represent the path of Windows directly. "C:\Windows\notepad.exe" can be written directly as "C:/windows/notepad.exe".

If the relative path is used here, the code using/representing the path will work correctly on two platforms.

Python represents a file path in a Windows system

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.