os.path.join

E

Elliot Peele

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

Elliot
 
7

7stud

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...
 
E

Elliot Peele

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
 
H

half.italian

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
 
G

Gabriel Genellina

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.
 
H

half.italian

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)

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
 
G

Gabriel Genellina

On May 1, 11:10 pm, "Gabriel Genellina" <[email protected]>
wrote:

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.html#SECTION006700000000000000000 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
 
A

Ant

On May 1, 11:10 pm, "Gabriel Genellina" <[email protected]> .... ....
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.
return a + b
return reduce(int.__add__, args)
4

The same sort of thing holds for keyword arguments:
for k in kw:
print kw[k]

1
100
10
 
H

half.italian

On May 1, 11:10 pm, "Gabriel Genellina" <[email protected]> ... ...
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.

return a + b

return reduce(int.__add__, args)

4

The same sort of thing holds for keyword arguments:

for k in kw:
print kw[k]

1
2>>> d = {'a': 1, 'b': 10, 'c': 100}
1
100
10

Thank you both.
 
T

Tim Roberts

Elliot Peele said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top