Talk about file path problems with Python in Windows

Source: Internet
Author: User
Below for everyone to share a discussion of Python in Windows file path problem, has a good reference value, I hope to be helpful to everyone. Come and see it together.

When you use Python to open a file by using the open () function, passing an absolute path to open (), you find that the contents of the path parameter differ from the imaginary:

A conflict may occur because the path delimiter for Windows uses a backslash \, which is just a good escape character

# Error Demonstration: Backslash appears to be interpreted as escape character instead of delimited character path = ' F:\Python\test.txt ' '    path = ' ' F:\\python\test.txt ' ' "" "" "" "" "" "" Path = ' F:\Python\\test.txt ' ' path = ' f:\\python\\test.txt ' ' path = ' ' F:\Python\\test.txt ' '    The last backslash of an absolute path uses a double backslash when it is normal ' ' ' because the last backslash can be an escape effect with the character t next to it

conclusion: when using an absolute path, be aware that the backslash used in the absolute path and the character immediately following it will not form an escape effect, and if it produces an escape effect, such as \ t \ r, and so on, you must use a double backslash to cancel the escape effect of the SLR slash. If an escape character is generated, an open () hint "Invalid arguments" will appear.

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.