os.path.join in python returns 'wrong' path? -
i have following python os.path output ipython
import os.path path path.join("/", "tmp") out[4]: '/tmp' path.join("/", "/tmp") out[5]: '/tmp' path.join("abc/", "/tmp") out[6]: '/tmp' path.join("abc", "/tmp") out[7]: '/tmp' path.join("/abc", "/tmp") out[8]: '/tmp' path.join("def", "tmp") out[10]: 'def/tmp' i find outputs 5, 6, 7 , 8 counterintuitive. can please explain if there specific reason implementation?
from os.path.join() documentation:
join 1 or more path components intelligently. if component absolute path, previous components (on windows, including previous drive letter, if there one) thrown away, , joining continues.
a / @ start makes /tmp absolute path.
if wanted join multiple path elements perhaps contain leading path separator, strip first:
os.path.join(*(elem.lstrip(os.sep) elem in elements)) special-casing absolute paths makes possible specify either relative path (from default parent directory) or absolute path , not have detect if have absolute or relative path when constructing final value.
Comments
Post a Comment