在我的機器上使用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