python - Joining: string and absolute path with os.path -


why not working, doing wrong?

>>> p1 = r'\foo\bar.txt' >>> os.path.join('foo1', 'foo2', os.path.normpath(p1)) '\\foo\\bar.txt' 

i expected this:

'foo1\\foo2\\foo\\bar.txt' 

edit:

a solution

>>> p1 = r'\foo\bar.txt' >>> p1 = p1.strip('\\') # strip '\\' path not absolute  >>> os.path.join('foo1', 'foo2', os.path.normpath(p1)) 'foo1\\foo2\\foo\\bar.txt' 

when os.path.join encounters absolute path, throws away has accumulated far. absolute string 1 starts slash (ans on windows, optional drive letter). normpath won't touch slash has same notion of absolute paths. have strip slash.

and if may ask: come in first place?


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -