How does os.path.join() work?

Viewed 15134

Please help me understand how the builtin os.path.join() function works. For example:

import os
print os.path.join('cat','dog') # 'cat/dog' no surprise here
print os.path.join('cat','dog').join('fish') # 'fcat/dogicat/dogscat/dogh'

On Mac (and i guess linux too) os.name is an alias for posixpath. So looking into the posixpath.py module, the join() function looks like this:

def join(a, *p):
"""Join two or more pathname components, inserting '/' as needed.
If any component is an absolute path, all previous path components
will be discarded.  An empty last part will result in a path that
ends with a separator."""
path = a
for b in p:
    if b.startswith('/'):
        path = b
    elif path == '' or path.endswith('/'):
        path +=  b
    else:
        path += '/' + b
return path

So join() returns a string. Why does os.path.join('something').join('something else') even work? Shouldn't it raise something like 'str' object has no attribute 'join'? I mean if I copy the function some other place and call it like renamed_join('foo','bar') it works as expected but if i do renamed_join('foo','bar').renamed_join('foobar') will raise an AttributeError as expected. Hopefully this is not a very stupid question. It struck me just when I thought I was starting to understand python...

2 Answers
Related