Reputation: 6762
I want to get a path location as ' \\BIWDB02\e$\research' using os.join.path
I tried these ways
import os
a = 'BIWDB02'
b = 'e$\research'
c = '\\\\'
print c
# \\
Try-1:
x = os.path.join('\\','\\',a,b)
print x
output:
\BIWDB02\e$
esearch
Don't know why it is coming on next line and even 'r' is missing.
Try-2 ,3
y = os.path.join('\\\\',a,b)
print y
z= os.path.join(c,a,b)
print z
Error:
IndexError: string index out of range
Update:
os.path.join('\\\\\\',a,b)
#\\\BIWDB02\e$\research
with 6-\\\ it gives me 3-\\ but with 4-\\ it gives me indexError again.
Upvotes: 2
Views: 99
Reputation: 7872
The issue is coming from the \r
in e$\research
. \r
is know as a carriage return and performs a return newline.
Add r
to e$\research
to make it a raw string literals
import os
a = 'BIWDB02'
b = r'e$\research'
c = '\\\\'
x = os.path.join(c, a, b)
print x
>>> \\BIWDB02\e$\research
Upvotes: 5
Reputation: 328
You don't have to manually escape your path names. You can cast them as raw strings in Python 2.x as follows:
"Path with lots of tricky characte\rs.\n..durr".encode('string-escape')
Upvotes: 0