By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,002 Members | 1,050 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,002 IT Pros & Developers. It's quick & easy.

os.path.join

P: n/a
Why does os.path.join('/foo', '/bar') return '/bar' rather than
'/foo/bar'? That just seems rather counter intuitive.

Elliot

May 2 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
On May 1, 7:36 pm, Elliot Peele <ell...@bentlogic.netwrote:
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...

May 2 '07 #2

P: n/a
On Tue, 2007-05-01 at 19:27 -0700, 7stud wrote:
On May 1, 7:36 pm, Elliot Peele <ell...@bentlogic.netwrote:
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

May 2 '07 #3

P: n/a
On May 1, 9:23 pm, Elliot Peele <ell...@bentlogic.netwrote:
On Tue, 2007-05-01 at 19:27 -0700, 7stud wrote:
On May 1, 7:36 pm, Elliot Peele <ell...@bentlogic.netwrote:
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

May 2 '07 #4

P: n/a
En Wed, 02 May 2007 01:23:45 -0300, Elliot Peele <el****@bentlogic.net>
escribió:
On Tue, 2007-05-01 at 19:27 -0700, 7stud wrote:
>On May 1, 7:36 pm, Elliot Peele <ell...@bentlogic.netwrote:
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?
It's not *how* it is written, but the current documentation for
os.path.join:
http://docs.python.org/lib/module-os.path.html#l2h-2176
It appears that the function docstring (used by the help system) is too
terse here.

--
Gabriel Genellina
May 2 '07 #5

P: n/a
En Wed, 02 May 2007 02:31:43 -0300, <ha**********@gmail.comescribió:
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.
I think it's a bug, but because it should raise TypeError instead.
The right usage is os.path.join(*pathparts)

--
Gabriel Genellina
May 2 '07 #6

P: n/a
On May 1, 11:10 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Wed, 02 May 2007 02:31:43 -0300, <half.ital...@gmail.comescribió:
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.

I think it's a bug, but because it should raise TypeError instead.
The right usage is os.path.join(*pathparts)

--
Gabriel Genellina
Wow. What exactly is that * operator doing? Is it only used in
passing args to functions? Does it just expand the list into
individual string arguments for exactly this situation? Or does it
have other uses?

~Sean

May 2 '07 #7

P: n/a
En Wed, 02 May 2007 04:03:56 -0300, <ha**********@gmail.comescribió:
On May 1, 11:10 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
>The right usage is os.path.join(*pathparts)

Wow. What exactly is that * operator doing? Is it only used in
passing args to functions? Does it just expand the list into
individual string arguments for exactly this situation? Or does it
have other uses?
When calling a function, it is used to pass a sequence as positional
arguments. Similarly, **values is used to pass a dictionary as keyword
arguments.
When defining a function, *args receives the remaining positional
arguments not already bound to another parameter; and **kwargs receives
the remaining keyword arguments not already bound to another parameter.
[There is nothing special on the *args and **kwargs names, only the * and
** are important]
See section 4.7 on the Python Tutorial
http://docs.python.org/tut/node6.htm...00000000000000 and
specially section 4.7.4 Unpacking Argument Lists.
For a more technical description (but sometimes necesary) read the Python
Reference Manual http://docs.python.org/ref/calls.html

--
Gabriel Genellina
May 2 '07 #8

P: n/a
Ant
On May 2, 8:03 am, half.ital...@gmail.com wrote:
On May 1, 11:10 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
....
I think it's a bug, but because it should raise TypeError instead.
The right usage is os.path.join(*pathparts)
....
Wow. What exactly is that * operator doing? Is it only used in
passing args to functions? Does it just expand the list into
individual string arguments for exactly this situation? Or does it
have other uses?
It's used for unpacking a collection into arguments to a function.
It's also used at the other end for receiving a variable length set of
arguments. i.e.
>>x = (1,3)
def add(a, b):
return a + b
>>add(*x)
4
>>def add(*args):
return reduce(int.__add__, args)
>>add(1,2,3,4,5,6)
21
>>add(*x)
4

The same sort of thing holds for keyword arguments:
>>def print_kw(**kw):
for k in kw:
print kw[k]

>>print_kw(a=1, b=2)
1
2
>>d = {'a': 1, 'b': 10, 'c': 100}
print_kw(**d)
1
100
10
May 2 '07 #9

P: n/a
On May 2, 12:36 am, Ant <ant...@gmail.comwrote:
On May 2, 8:03 am, half.ital...@gmail.com wrote:
On May 1, 11:10 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
...
I think it's a bug, but because it should raise TypeError instead.
The right usage is os.path.join(*pathparts)
...
Wow. What exactly is that * operator doing? Is it only used in
passing args to functions? Does it just expand the list into
individual string arguments for exactly this situation? Or does it
have other uses?

It's used for unpacking a collection into arguments to a function.
It's also used at the other end for receiving a variable length set of
arguments. i.e.
>x = (1,3)
def add(a, b):

return a + b
>add(*x)
4
>def add(*args):

return reduce(int.__add__, args)
>add(1,2,3,4,5,6)
21
>add(*x)

4

The same sort of thing holds for keyword arguments:
>def print_kw(**kw):

for k in kw:
print kw[k]
>print_kw(a=1, b=2)

1
2>>d = {'a': 1, 'b': 10, 'c': 100}
>print_kw(**d)

1
100
10
Thank you both.

May 2 '07 #10

P: n/a
Elliot Peele <el****@bentlogic.netwrote:
>On Tue, 2007-05-01 at 19:27 -0700, 7stud wrote:
>On May 1, 7:36 pm, Elliot Peele <ell...@bentlogic.netwrote:
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?
It's behavior is exactly the same as if you did a series of "cd" commands
at the shell with the same parameters.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
May 4 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.