os.path.join("/a","/b")

Fredrik Lundh fredrik at pythonware.com
Thu Feb 22 17:36:09 EST 2001


Matthew Dixon Cowles wrote:
> I was a little surprised today to notice that
>
> os.path.join("/a","/b") returns '/b'
>
> (os.path is posixpath in this case of course) while
>
> os.path.join("/a/","b") returns "/a/b"
>
> which I expected from both. Poking back into my dusty archives, I find
> that it has been that way since at least 1.4 so I presume that it's a
> feature but I'll be darned if I can figure out the logic.

http://www.python.org/doc/current/lib/module-os.path.html

    join (path1[, path2[, ...]])

    Joins one or more path components intelligently. If any
    component is an absolute path, all previous components
    are thrown away, and joining continues. The return value
    is the concatenation of path1, and optionally path2, etc.,
    with exactly one slash ('/') inserted between components,
    unless path is empty.

the rationale is probably that it's a good idea if

    os.path.join(os.getcwd(), filename)

refers to the same file as:

    filename

for any filename.

Cheers /F

<!-- (the eff-bot guide to) the standard python library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->







More information about the Python-list mailing list