Skip to content

Instantly share code, notes, and snippets.

@maphew
Last active February 21, 2016 20:30
Show Gist options
  • Save maphew/9368fe16df751b016bbd to your computer and use it in GitHub Desktop.
Save maphew/9368fe16df751b016bbd to your computer and use it in GitHub Desktop.
python 2.7.8 broken raw string?
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))
@maphew
Copy link
Author

maphew commented Feb 19, 2016

result from PyScripter and remote python interpreter

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

and command shell Python 3:

> 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```

@maphew
Copy link
Author

maphew commented Feb 21, 2016

Raw is doing the right thing, what's happening is that os.path.join disregards everything preceding the argument with a leading slash (arg is treated as root element). See http://stackoverflow.com/questions/35516674/python-raw-string-literal-with-slash-w-r-w/35517343#35517343

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment