os.path.join

half.italian at gmail.com half.italian at gmail.com
Wed May 2 01:31:43 EDT 2007


On May 1, 9:23 pm, Elliot Peele <ell... at bentlogic.net> wrote:
> On Tue, 2007-05-01 at 19:27 -0700, 7stud wrote:
> > On May 1, 7:36 pm, Elliot Peele <ell... at bentlogic.net> wrote:
> > > Why does os.path.join('/foo', '/bar') return '/bar' rather than
> > > '/foo/bar'? That just seems rather counter intuitive.
>
> > > Elliot
>
> > join(      path1[, path2[, ...]])
> > Join one or more path components intelligently. If any component is an
> > absolute path, all previous components (on Windows, including the
> > previous drive letter, if there was one) are thrown away...
>
> Yes, but that still doesn't answer my question as to why os.path.join
> works that way. I understand that that is how it is written, but why?
>
> Elliot

It makes perfect sense. You are joining two paths that both begin at
the root directory.  The second path is overwriting the first because
they can't both begin at the root and also be parts of one path.

A better question is why this doesn't work.

>>> pathparts = ["/foo", "bar"]
>>> os.path.join(pathparts)
['/foo', 'bar']

This should return a string in my opinion.

~Sean




More information about the Python-list mailing list