在我的机器上使用python 2.7.8和3.4时,如果我在原始字符串文字中有一个反斜杠-W,它不会被视为原始数据。这真的是预期的行为?Python原始字符串文字与斜杠-r-r` 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解释器中获得。备注9W
vs 9\W
和D:\Feb
vs D:\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.join()'也规范化路径。它将反斜杠视为路径加密器。当它不是*那里时,它会放在那里。 – zondo