Strange path separators on Windows

前端 未结 6 911
臣服心动
臣服心动 2020-12-18 07:44

I an running this code:

#!/usr/bin/python      coding=utf8
#  test.py = to demo fault
def loadFile(path):
    f = open(path,\'r\')
    text = f.read()
    re         


        
6条回答
  •  旧巷少年郎
    2020-12-18 08:22

    The backslash is an escape character when the next character combination would result in a special meaning. Take the following examples:

    >>> '\r'
    '\r'
    >>> '\n'
    '\n'
    >>> '\b'
    '\x08'
    >>> '\c'
    '\\c'
    >>>
    

    r, n, and b all have special meanings when preceded by a backslash. The same is true for t, which would produce a tab. You either need to A. Double all your backslashes, for consistency, because '\\' will produce a backslash, or, B, use raw strings: r'c:\path\to\my\file.txt'. The preceding r will prompt the interpreter not to evaluate back slashes as escape sequences, preventing the \t from appearing as a tab.

提交回复
热议问题