Python 带有斜杠-w 的原始字符串文字 -- r`\w`

Python raw string literal with slash-w -- r`\w`

在我的机器上使用 python 2.7.8 和 3.4,当我在原始字符串文字中有一个反斜杠-W 时,它不会被视为原始字符串。这真的是预期的行为吗?

import os
import sys
wspace = r'D:\Feb-19'
tile = '116o'
ex1 = os.path.join(wspace, r'Work_{}\scratch.gdb'.format(tile))
ex2 = os.path.join(wspace, r'\Work_{}\scratch.gdb'.format(tile))

print(sys.version)

print('''\n--- Expected ---
no-slash-W      D:\Feb-19Work_116o\scratch.gdb
yes-slash-W     D:\Feb-19\Work_116o\scratch.gdb
''')

print('''--- Actual Result ---
no-slash-W      {}
yes-slash-W     {}
'''.format(ex1, ex2))

我从 PyScripter 和远程 python 解释器得到的结果。注意 9W9\WD:\FebD:\Work.

2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)]
--- Expected ---
no-slash-W      D:\Feb-19Work_116o\scratch.gdb
yes-slash-W     D:\Feb-19\Work_116o\scratch.gdb

--- Actual Result ---
no-slash-W      D:\Feb-19\Work_116o\scratch.gdb
yes-slash-W     D:\Work_116o\scratch.gdb

...和命令 shell Python 3:

D:\> python broken-raw-string-example.py
3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:45:13) [MSC v.1600 64 bit (AMD64)]

--- Expected ---
no-slash-W      D:\Feb-19Work_116o\scratch.gdb
yes-slash-W     D:\Feb-19\Work_116o\scratch.gdb

--- Actual Result ---
no-slash-W      D:\Feb-19\Work_116o\scratch.gdb
yes-slash-W     D:\Work_116o\scratch.gdb

如果你想在其中插入叉子,请看这里: https://gist.github.com/maphew/9368fe16df751b016bbd

这里不是原始字符串让你绊倒;你误解了 os.path.joinos.path.join 应该 在组件不以 1 开头时添加斜线。如果斜杠已经存在,它会被视为绝对路径的开始,它会丢弃前面的组件并“从头开始”重新开始(除了 Windows 上的驱动器号)。来自文档:

If a component is an absolute path, all previous components are thrown away and joining continues from the absolute path component.

On Windows, the drive letter is not reset when an absolute path component (e.g., r'\foo') is encountered. If a component contains a drive letter, all previous components are thrown away and the drive letter is reset. Note that since there is a current directory for each drive, os.path.join("c:", "foo") represents a path relative to the current directory on drive C: (c:foo), not c:\foo.

所以这是预期的并且是正常的:

os.path.join('C:\a', 'b')

在 Windows 上生成字符串 C:\a\b(由于反斜杠的必要转义,其 repr 将是 "C:\a\b"),同时:

os.path.join('C:\a', '\b')

'\b' 的意思是“从当前驱动器开始一个新的绝对路径”并丢弃 \a 替换为 \b。同样,

os.path.join('C:\a', 'b', '\c', 'd')

会在看到 '\c' 时丢弃 ab 并从那里建立路径,生成 C:\c\d.